1, FileWritter writes files
FileWritter, the character stream writes characters to the file. By default, it will replace all existing content with new content, however, when a true (boolean) value is specified as the second parameter of the FileWritter constructor, it will retain the existing content and append the new content in end of file.
1. Replace all existing content with new content.
new FileWriter(file);2. Keep the existing content and append the new content to the end of the file.
new FileWriter(file,true);
Append file example
A text file named "javaio-appendfile.txt" and contains the following content.
ABC Hello append new content new FileWriter(file,true)
package com.yiibai.file; import java.io.File; import java.io.FileWriter; import java.io.BufferedWriter; import java.io.IOException; public class AppendToFileExample { public static void main( String[] args ) { try{ String data = " This content will append to the end of the file"; File file =new File("javaio-appendfile.txt"); //if file doesnt exists, then create it if(!file.exists()){ file.createNewFile(); } //true = append file FileWriter fileWritter = new FileWriter(file.getName(),true); BufferedWriter bufferWritter = new BufferedWriter(fileWritter); bufferWritter.write(data); bufferWritter.close(); System.out.println("Done"); }catch(IOException e){ e.printStackTrace(); } } }
Result
Now, the content of the text file "javaio-appendfile.txt" is updated as follows:
ABC Hello This content will append to the end of the file
Second, BufferedWriter writes to the file
Buffered characters (BufferedWriter) is a character stream class to process character data. Unlike byte streams (data converted into bytes), you can write string, array or character data directly to a file.
package com.yiibai.iofile; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteToFileExample { public static void main(String[] args) { try { String content = "This is the content to write into file"; File file = new File("/users/mkyong/filename.txt"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }
Three, FileOutputStream writes to a file
The file output stream is a byte stream class used to process raw binary data. In order to write data to a file, the data must be converted into bytes and saved to the file. See the complete example below.
package com.yiibai.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileExample { public static void main(String[] args) { FileOutputStream fop = null; File file; String content = "This is the text content"; try { file = new File("c:/newfile.txt"); fop = new FileOutputStream(file); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } // get the content in bytes byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fop != null) { fop.close(); } } catch (IOException e) { e.printStackTrace(); } } } } //更新的JDK7例如,使用新的“尝试资源关闭”的方法来轻松处理文件。 package com.yiibai.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileExample { public static void main(String[] args) { File file = new File("c:/newfile.txt"); String content = "This is the text content"; try (FileOutputStream fop = new FileOutputStream(file)) { // if file doesn't exists, then create it if (!file.exists()) { file.createNewFile(); } // get the content in bytes byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }
For more java methods of writing files to share related articles, please pay attention to the PHP Chinese website!