"reference" http://www.cnblogs.com/zhuocheng/archive/2011/12/12/2285290.html
I. Obtaining information from console user input publicString Getinputmessage ()throwsIOException ... {System.out.println ("please Enter your command:"); bytebuffer[]=New byte[1024]; intCount=System.in.read (buffer); Char[] ch=New Char[count-2];//the last two bits are the terminator, delete the for(inti=0;i<count-2;i++) ch[i]=(Char) buffer[i]; String Str=NewString (ch); returnstr; The information entered by the user can be returned, but the disadvantage is that Chinese input is not supported and needs further Improvement. Two. Copying Files1. Copying files as a file stream public voidCopyFile (String src,string Dest)throwsIOException ... {fileinputstream in=NewFileInputStream (src); File File=NewFile (dest); if(!file.exists ()) file.createnewfile (); FileOutputStream out=NewFileOutputStream (file); intc; bytebuffer[]=New byte[1024]; while(c=in.read (buffer))!=-1)... { for(inti=0;i<c;i++) Out.write (buffer[i]); } in.close (); Out.close (); This method is tested, supports Chinese processing, and can replicate many types, such as txt,xml,jpg,doc, etc. three. writing files1. Writing files with PrintStream public voidPrintstreamdemo () ... { Try ... {fileoutputstream out=NewFileOutputStream ("d:/test.txt"); PrintStream P=NewPrintStream (out); for(inti=0;i<10;i++) P.println ("this was" +i+ "line"); } Catch(filenotfoundexception E) ... {e.printstacktrace (); } } 2. Writing files with StringBuffer public voidStringbufferdemo ()throwsIOException ... {file file=NewFile ("/root/sms.log"); if(!file.exists ()) file.createnewfile (); FileOutputStream out=NewFileOutputStream (file,true); for(inti=0;i<10000;i++)...... {stringbuffer SB=NewStringBuffer (); Sb.append ("this is the first" +i+ "line: the various methods described earlier are not used, why is it always a strange question"); Out.write (sb.tostring (). getBytes ("utf-8")); } out.close (); This method can set what encoding to use to effectively solve the Chinese Problem. Four. file renaming public voidrenamefile (String path,string oldname,string Newname) ... { if(!oldname.equals (newname)) ... {//the new file name and the previous file name are not the same, it is necessary to renameFile oldfile=NewFile (path+ "/" +oldname); File NewFile=NewFile (path+ "/" +newname); if(newfile.exists ())//If there is already a file and the new file name in the directory, renaming is not allowedSystem.out.println (newname+ "already exists! "); Else... {oldfile.renameto (newfile); }}} Five. The Transfer file directory transfer file directory is not the same as copying files, the copy file is copied after both directories exist for the file, and the transfer file directory is transferred, only the new directory exists in the File. public voidChangedirectory (String filename,string oldpath,string newpath,Booleancover) ... { if(!oldpath.equals (newpath)) ... {File Oldfile=NewFile (oldpath+ "/" +filename); File NewFile=NewFile (newpath+ "/" +filename); if(newfile.exists ()) ... {//if the file to be transferred is already present in the pending transfer directory if(cover)//coverOldfile.renameto (newfile); ElseSystem.out.println ("already exists under the new directory:" +filename); } Else... {oldfile.renameto (newfile); }}} six. Read the file1. reading files with FileInputStream publicString Fileinputstreamdemo (string Path)throwsIOException ... {file file=NewFile (path); if(!file.exists () | |file.isdirectory ())Throw NewFileNotFoundException (); FileInputStream FIS=NewFileInputStream (file); byte[] buf =New byte[1024]; StringBuffer SB=NewStringBuffer (); while((fis.read (buf))!=-1)... {sb.append (NewString (buf)); BUF=New byte[1024];//regenerate to avoid duplication with Last-read data } returnsb.tostring (); }2using BufferedReader read in IO operation, using BufferedReader and bufferedwriter efficiency will be a bit higher publicString Bufferedreaderdemo (string Path)throwsIOException ... {file file=NewFile (path); if(!file.exists () | |file.isdirectory ())Throw NewFileNotFoundException (); BufferedReader BR=NewBufferedReader (NewFileReader (file)); String Temp=NULL; StringBuffer SB=NewStringBuffer (); Temp=Br.readline (); while(temp!=NULL)... {sb.append (temp+" "); Temp=Br.readline (); } returnsb.tostring (); } 3. Reading an XML file with dom4j publicDocument readXml (String Path)throwsdocumentexception, IOException ... {file file=NewFile (path); BufferedReader BufferedReader=NewBufferedReader (NewFileReader (file)); Saxreader Saxreader=NewSaxreader (); Document Document=(Document) Saxreader.read (bufferedreader); Bufferedreader.close (); returndocument; } seven. Create a file (folder)1. Create a folder public voidcreatedir (String Path) ... {File dir=NewFile (path); if(!dir.exists ()) Dir.mkdir (); }2. Create a new file public voidCreateFile (String path,string Filename)throwsIOException ... {file file=NewFile (path+ "/" +filename); if(!file.exists ()) file.createnewfile (); } eight. deleting files (directories)1. deleting files public voiddelfile (String path,string Filename) ... {file file=NewFile (path+ "/" +filename); if(file.exists () &&file.isfile ()) File.delete (); }2Delete a directory to delete a directory with the Delete () method of the file class, you must ensure that there are no files or subdirectories in the directory, otherwise the deletion fails, so in the actual application, we want to delete the directory, we must use recursion to delete all subdirectories and files under that directory, and then delete the Directory. public voiddeldir (String Path) ... {File dir=NewFile (path); if(dir.exists ()) ... {file[] tmp=Dir.listfiles (); for(inti=0;i<tmp.length;i++)... { if(tmp[i].isdirectory ()) ... {deldir (path+"/"+Tmp[i].getname ()); } Else... {tmp[i].delete (); }} Dir.delete (); }}from Zhuocheng (cnblogs.com/zhuocheng)
Java file read and write operations Daquan