Write to File
One, FileWriter and BufferedWriter write files
FileWriter is a character stream that writes characters to a file. By default, it uses the new content instead of all the original contents of the file, but when you specify a true value as the second parameter of the FileWriter constructor, it preserves the existing content and appends the new content to the end of the file.
BufferedWriter: Buffer character, is a character stream class to handle character data. Unlike byte streams (data converted to bytes FileOutputStream), you can write strings, arrays, or character data directly to a file.
By default, replace the original content: new FileWriter (file); Keep the original content: new FileWriter (file,true)
Specific examples are as follows:
Public Static voidWRITEINFILEBYFB () {File F=NewFile ("E:\\java\\jmoa\\testdiff\\src\\test\\resource\\test_fb.txt"); String content="new content to write to the file"; FileWriter FW=NULL; BufferedWriter BW=NULL; Try{ if(!f.exists ()) {F.createnewfile (); } FW=NewFileWriter (F.getabsolutefile (),true);//True indicates that new content can be appended//fw=new FileWriter (F.getabsolutefile ());//indicates no appendbw=NewBufferedWriter (FW); Bw.write (content); Bw.close (); }Catch(Exception e) {e.printstacktrace (); } }
Second, FileOutputStream byte stream write file
The file output stream is a byte of tears that is used to process raw binary data. In order to write data to a file, the data must be converted to bytes and saved to a file. Specific examples are as follows:
Package Com.joyplus.test;import java.io.file;import java.io.fileoutputstream;import java.io.IOException; Public classFileoutputtest { Public Static voidMain (string[] args) {Fileoutputtest.writeinfilebyfi (); } Public Static voidWriteinfilebyfi () {File F=NewFile ("E:\\java\\jmoa\\testdiff\\src\\test\\resource\\test_fi.txt"); FileOutputStream Fos=NULL; Try { if(!f.exists ()) {F.createnewfile (); } Fos=NewFileOutputStream (f); String content="the new content to write! "; Fos.write (Content.getbytes ()); } Catch(IOException e) {e.printstacktrace (); }finally{ if(fos!=NULL){ Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); } } } }}
Third, randomaccessfile write file
The only parent class for Randomaccessfile is object, which is different from other stream parent classes. is used to access files that hold data records, so you can use the Seek () method to access the records and read and write them. The sizes of these records do not have to be the same, but their size and position must be knowable.
The following example is an example of how Randomaccessfile writes to a file:
Public Static voidWriteinfilebyrda () {String content="Randowaccessfile"; Try{ //open a random Access file stream, read and writeRandomaccessfile Randomfile =NewRandomaccessfile ("E:\\java\\jmoa\\testdiff\\src\\test\\resource\\test_fb.txt","RW"); //file length, number of bytes LongFilelength =randomfile.length (); //moves the write file pointer to the end of the file. Randomfile.seek (filelength); Randomfile.writebytes (content); Randomfile.close (); }Catch(Exception e) {e.printstacktrace (); } }
Read the file
One, FileInputStream byte stream read the file "Note: When reading Chinese will be garbled"
The specific code is as follows:
//read file contents by byte Public Staticstring Readfilebybyte () {string S=""; File F=NewFile ("E:\\java\\jmoa\\testdiff\\src\\test\\resource\\test_fb.txt"); InputStreaminch=NULL; Try{ inch=NewFileInputStream (f); intTempbyte; while((tempbyte=inch. Read ())!=-1) {System. out. println (Tempbyte); S+=Tempbyte; } inch. Close (); }Catch(Exception e) {e.printstacktrace (); } System. out. println ("content:"+s); returns; }
Second, inputstreamreader character stream read the contents of the file
//read file contents by character Public Staticstring Readfilebychar () {string S=""; File F=NewFile ("E:\\java\\jmoa\\testdiff\\src\\test\\resource\\test_fb.txt"); Reader RDR=NULL; Try{RDR=NewInputStreamReader (NewFileInputStream (f)); inttemp; while((Temp=rdr.read ())!=-1){ //for window, \ r \ n These two characters are together to represent a newline. //However, if the two characters are displayed separately, two lines will be changed. //therefore, shield the \ r, or shield off \ n. Otherwise, there will be a lot of empty lines if(((Char) temp)! ='\ r') {s+=(Char) temp; } } }Catch(Exception e) {e.printstacktrace (); }finally{ Try{rdr.close (); } Catch(IOException e) {e.printstacktrace (); }} System. out. println (s); returns; }
Third, BufferedReader read the file content in the behavior unit
//read files by line Public Staticstring Readfilebyline () {string S=""; File F=NewFile ("E:\\java\\jmoa\\testdiff\\src\\test\\resource\\test_fb.txt"); BufferedReader BR=NULL; Try{System. out. println ("read file contents by line"); BR=NewBufferedReader (NewFileReader (f)); String temp; while((Temp=br.readline ())! =NULL) {s+=temp; } }Catch(Exception e) {e.printstacktrace (); }finally{ Try{br.close (); } Catch(IOException e) {e.printstacktrace (); }} System. out. println ("file content:"+s); returns; }
Iv. Random Read some of the contents of the file: Randomaccessfile
//Random line Read file Public Staticstring Readfilebyrand () {string S=""; File F=NewFile ("E:\\java\\jmoa\\testdiff\\src\\test\\resource\\test_fb.txt"); Randomaccessfile RAF=NULL; Try{ //opens a file stream, as read-onlyTA =NewRandomaccessfile (F.getname (),"R"); //file length, number of bytes LongFilelength=raf.length (); //the starting position of the read file intBeginindex= (filelength>4)?4:0; //move the starting position of the read file to the Beginindex locationRaf.seek (Beginindex); byte[] bytes=New byte[Ten]; intByteread=0; //read 10 bytes at a time, and if the contents of the file are less than 10 bytes, read the remaining text. //assigns the number of bytes read to Byteread while((Byteread=raf.read (bytes))!=-1) {System. out. Write (Bytes,0, Byteread); } }Catch(Exception e) {e.printstacktrace (); }finally{ if(raf!=NULL){ Try{raf.close (); } Catch(IOException e) {e.printstacktrace (); } } } //System.out.println ("File contents:" +s); returns; }
Supplemental Documentation Knowledge:
/* * * shows the number of bytes remaining in the input stream */ privatestaticvoid in { try { System]. out. println (" the number of bytes in the current byte input stream is:"in. Available () ); Catch (IOException e) { e.printstacktrace (); } }
Writing and reading of Java file files