Java附加内容到文件是一种常见的Java IO操作。例如,每当将某些内容写入到服务器日志时,就是将内容附加到现有文件中过程。
在java中可以使用以下类附加到文件。
- 使用
FileWriter
附加内容到文件; - 使用
BufferedWriter
将内容附加到文件; - 使用
PrintWriter
将文本附加内容到文件; - 使用
FileOutputStream
附加内容到文件;
如果处理文本数据并且写入操作的数量较少,最好使用FileWriter
并使用其构造函数将append
的flag
值设置为true
。如果写入操作的数量很大,则应使用BufferedWriter
。
要将二进制或原始流数据附加到现有文件,应该使用FileOutputStream
。
第1种方式: 使用FileWriter附加到文件
下面是使用FileWriter
附加到文件的简短程序。
File file = new File("append.txt");
FileWriter fr = new FileWriter(file, true);
fr.write("data");
fr.close();
第2种方式: 使用BufferedWriter
将内容附加到现有文件
File file = new File("append.txt");
FileWriter fr = new FileWriter(file, true);
BufferedWriter br = new BufferedWriter(fr);
br.write("data");
br.close();
fr.close();
第3种方式: 使用PrintWriter将文本附加内容到文件
File file = new File("append.txt");
FileWriter fr = new FileWriter(file, true);
BufferedWriter br = new BufferedWriter(fr);
PrintWriter pr = new PrintWriter(br);
pr.println("data");
pr.close();
br.close();
fr.close();
第3种方式: 使用FileOutputStream追加到文件
当原始数据,二进制数据,图像,视频等应该使用FileOutputStream
将数据附加到文件中。
OutputStream os = new FileOutputStream(new File("append.txt"), true);
os.write("data".getBytes(), 0, "data".length());
os.close();
Java附加到文件示例
下面是附加内容到文件的完整示例程序,显示了上面讨论的几种方式。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
public class JavaAppendToFile {
/**
* 将内容附加到文件的示例
*
* @param args
*/
public static void main(String[] args) {
String filePath = "D:/users/maxsu/append.txt";
String appendText = "This String will be appended to the file, Byte=0x0A 0xFF";
appendUsingFileWriter(filePath, appendText);
appendUsingBufferedWriter(filePath, appendText, 2);
appendUsingPrintWriter(filePath, appendText);
appendUsingFileOutputStream(filePath, appendText);
}
private static void appendUsingPrintWriter(String filePath, String text) {
File file = new File(filePath);
FileWriter fr = null;
BufferedWriter br = null;
PrintWriter pr = null;
try {
// 要附加到文件,需要使用下面的构造函数初始化FileWriter
fr = new FileWriter(file, true);
br = new BufferedWriter(fr);
pr = new PrintWriter(br);
pr.println(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
pr.close();
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 在处理原始数据二进制时,使用Stream追加到文件
* data
*
* @param data
*/
private static void appendUsingFileOutputStream(String fileName, String data) {
OutputStream os = null;
try {
// 下面true标志告诉OutputStream追加
os = new FileOutputStream(new File(fileName), true);
os.write(data.getBytes(), 0, data.length());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 当写操作次数更多时,使用BufferedWriter
*
* @param filePath
* @param text
* @param noOfLines
*/
private static void appendUsingBufferedWriter(String filePath, String text, int noOfLines) {
File file = new File(filePath);
FileWriter fr = null;
BufferedWriter br = null;
try {
// 要附加到文件,需要使用下面的构造函数初始化FileWriter
fr = new FileWriter(file, true);
br = new BufferedWriter(fr);
for (int i = 0; i < noOfLines; i++) {
br.newLine();
// you can use write or append method
br.write(text);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 当写操作次数较少时使用FileWriter
*
* @param filePath
* @param text
* @param noOfLines
*/
private static void appendUsingFileWriter(String filePath, String text) {
File file = new File(filePath);
FileWriter fr = null;
try {
// 下面的构造函数参数决定是追加还是覆盖
fr = new FileWriter(file, true);
fr.write(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
上面就是在java程序中附加数据到文件的全部内容。