1, File Create delete name change path
1 PackageStream;2 3 ImportJava.io.File;4 5 Importjava.io.IOException;6 7 Public classOutin {8 9 /**Ten * Input/output One */ A Public Static voidMain (string[] args) { -File F1 =NewFile ("D:/book.txt");//the first instantiation file name escape sequence is invalid (valid value is \b \ n - //\f \ \ \ \ \ \) the //file F1=new file ("d:/", "Book.txt");//second file path and filename notation - if(F1.exists ()) {//determine if there is -System.out.println ("File already exists"); - //F1.delete ();//files can be deleted if they exist +}Else { - Try { +F1.createnewfile ();//does not exist create file A}Catch(IOException e) { at //TODO Auto-generated catch block - e.printstacktrace (); - } -F1.renameto (NewFile ("C:/buk.txt"));//file name and file path can be changed if the files already exist - } - in } -}
2, output stream does not overwrite line break input
1 PackageStream;2 3 ImportJava.io.File;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8 Public classOutIn1 {9 Ten One Public Static voidMain (string[] args) { AFile ff=NewFile ("D:/book.txt");//output stream in the presence of Book.txt on D-Disk - -FileOutputStream shuchu=NULL; //Prevent the following finally in the Shuchu unrecognized initialization drop out of the try try { - - //shuchu=new FileOutputStream (FF);//The first output is written to overwrite the write instantiation of the binary stream -shuchu=NewFileOutputStream (FF,true);//second typeoutput notation Append write parameter multiple Boolean parameters + -String mm= "monkey \ n";//Create string wrap input plus \ n + byte[]b=mm.getbytes ();//Passed the string. GetBytes () method to array B A Try { atShuchu.write (b);//Array b content is uploaded to D:/book.txt to complete the write - - -}Catch(IOException e) { - //TODO Auto-generated catch block - e.printstacktrace (); in } -}Catch(FileNotFoundException e) { to //TODO Auto-generated catch block + e.printstacktrace (); - } the finally{//The try Catch finally captures the exception structure guarantee that the output stream can be closed even if there is an exception earlier * Try { $Shuchu.close ();//output complete to close it or the output stream has been exclusive of this file other threads are unable to manipulate the filePanax Notoginseng}Catch(IOException e) { - //TODO Auto-generated catch block the e.printstacktrace (); + } A } the + } - $}
3, input stream
1 PackageStream;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.FileNotFoundException;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 9 Public classOutIn2 {Ten One A Public Static voidMain (string[] args)throwsFileNotFoundException { -File ff=NewFile ("D:/book.txt");//determine if file exists with ff.exists () to verify existence -FileInputStream f2=NewFileInputStream (FF);//exception is required in case of presence the //fileinputstream f2=new fileinputstream ("D:/book.txt");//The second way of writing can omit the file instantiation process - byte[]c=New byte[1024];//cannot know beforehand how large an array 1024 is an estimate - //in order to be able to read below first build an empty array - Try { + - intChang=f2.read (c);//read array c return length +String s=NewString (C,0,chang);//the contents of the read are converted to string method names and horizontal lines indicate obsolete methods A //from the Reading method to get the length of the word by the control of the length of more than 1024 of the removal of the expression as a string behind the white space without a cursor atSystem.out.println ("Content in the text:" +s);//Enter what you read -}Catch(IOException e) { - //TODO Auto-generated catch block - e.printstacktrace (); - } - finally{ in Try { -F2.close ();//pay attention to the closing of the convection or else the thread cannot manipulate the file once the previous error occurs to}Catch(IOException e) { + //TODO Auto-generated catch block - e.printstacktrace (); the } * } $ }Panax Notoginseng -}
2016/1/27 1, File create delete name change path 2, output stream does not overwrite line break input 3, input stream