Additional java file

public  class AppendToFile { 
 / **  
* A method for the additional file: Use a RandomAccessFile 
* /  
public  static  void appendMethodA (fileName String, String Content) { 
 the try { 
 // Open a random access file stream, both reading and writing 
a RandomAccessFile randomfile = new new a RandomAccessFile ( fileName, "RW" ); 
 // file length, the number of bytes 
Long fileLength = randomFile.length (); 
 // write the file pointer to the end of the file. 
randomFile.seek (fileLength); 
randomFile.writeBytes (Content); 
randomFile.close (); 
} the catch (IOException E) { 
e.printStackTrace (); 
}
} 

/ **  
* the additional file Method B: Use FileWriter 
* /  
public  static  void appendMethodB (fileName String, String Content) { 
 the try { 
 // Open a file is written, the second constructor parameter additionally written in the form of true indicates file 
FileWriter Writer = new new FileWriter (fileName, to true ); 
writer.Write (Content); 
writer.Close (); 
} the catch (IOException E) { 
e.printStackTrace (); 
} 
} 

public  static  void main (String [] args) { 
String fileName = "C: /temp/newTemp.txt" ; 
String Content = "the append new new!"; 
 // Method A append file 
AppendToFile.appendMethodA (fileName, Content); 
AppendToFile.appendMethodA (fileName, "the append End \ n-." ); 
 // file contents 
ReadFromFile.readFileByLines (fileName); 
 // by adding Method B file 
AppendToFile.appendMethodB (fileName, content); 
AppendToFile.appendMethodB (fileName, "the append End \ n-." ); 
 // file contents 
ReadFromFile.readFileByLines (fileName); 
} 
}

 

Guess you like

Origin www.cnblogs.com/JonaLin/p/11016479.html