java追加到文件末尾
Today we will look into how to append to a file in java. Java append to file is a common java IO operation. For example, whenever we print something to server logs, it gets appended to the existing file.
今天,我们将研究如何在Java中附加文件。 Java追加到文件是一种常见的Java IO操作。 例如,每当我们将某些内容打印到服务器日志时,它就会被附加到现有文件中。
Java追加到文件 (Java append to file)
We can append to file in java using following classes.
我们可以使用以下类在Java中追加文件。
- Java append to file using
FileWriterJava使用FileWriter附加到文件 - Java append content to existing file using
BufferedWriterJava使用BufferedWriter将内容追加到现有文件 - Append text to file in java using
PrintWriter使用PrintWriter将文本追加到Java中的文件 - Append to file in java using
FileOutputStream使用FileOutputStream附加到Java中的文件
If you are working on text data and the number of write operations is less, use FileWriter and use its constructor with append flag value as true. If the number of write operations is huge, you should use the BufferedWriter.
如果您正在处理文本数据并且写入操作的次数较少,请使用FileWriter并将其构造函数的附加标志值设置为true 。 如果写入操作的数量很多,则应使用BufferedWriter 。
To append binary or raw stream data to an existing file, you should use FileOutputStream.
要将二进制或原始流数据附加到现有文件,应使用FileOutputStream。
Java使用FileWriter附加到文件 (Java append to file using FileWriter)
Here is the short program to append to file in java using FileWriter. We will look into a complete Java append to file example program later on.
这是使用FileWriter附加到java中文件的简短程序。 稍后,我们将研究完整的Java追加到文件示例程序。
File file = new File("append.txt");
FileWriter fr = new FileWriter(file, true);
fr.write("data");
fr.close();Java使用BufferedWriter将内容追加到现有文件 (Java append content to existing file using 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();使用PrintWriter将文本追加到Java中的文件 (Append text to file in java using PrintWriter)
We can also use PrintWriter to append to file in java.
我们还可以使用PrintWriter附加到java中的文件。
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();使用FileOutputStream附加到Java中的文件 (Append to file in java using FileOutputStream)
You should use FileOutputStream to append data to file when it’s raw data, binary data, images, videos etc.
当数据是原始数据,二进制数据,图像,视频等时,应使用FileOutputStream将数据追加到文件中。
OutputStream os = new FileOutputStream(new File("append.txt"), true);
os.write("data".getBytes(), 0, "data".length());
os.close();Java附加到文件示例 (Java append to file example)
Here is the final java append to file program showing all the different options we discussed above.
这是文件程序的最后java追加,显示了我们上面讨论的所有不同选项。
package com.journaldev.files;
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 {
/**
* Java append to file example
*
* @param args
*/
public static void main(String[] args) {
String filePath = "/Users/pankaj/Downloads/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 {
// to append to file, you need to initialize FileWriter using below constructor
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();
}
}
}
/**
* Use Stream for java append to file when you are dealing with raw data, binary
* data
*
* @param data
*/
private static void appendUsingFileOutputStream(String fileName, String data) {
OutputStream os = null;
try {
// below true flag tells OutputStream to append
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();
}
}
}
/**
* Use BufferedWriter when number of write operations are more
*
* @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 {
// to append to file, you need to initialize FileWriter using below constructor
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();
}
}
}
/**
* Use FileWriter when number of write operations are less
*
* @param filePath
* @param text
* @param noOfLines
*/
private static void appendUsingFileWriter(String filePath, String text) {
File file = new File(filePath);
FileWriter fr = null;
try {
// Below constructor argument decides whether to append or override
fr = new FileWriter(file, true);
fr.write(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}That’s all for append to file in java program.
这就是将所有内容追加到java程序中的文件。
References:
参考文献:
- PrintWriter API DocPrintWriter API文档
- FileWriter API DocFileWriter API文档
- BufferedWriter API DocBufferedWriter API文档
- FileOutputStream API DocFileOutputStream API文档
java追加到文件末尾