使用类的append()方法可以实现文件写入不覆盖的功能,是一个示例代码:import java.io.*;
使用FileWriter类的append()方法可以实现文件写入不覆盖的功能,是一个示例代码:
import java.io.*;
public class AppendToFileExample {
public static void main(String[] args) {
try {
String content = "This is my content which would be appended at the end of the file";
File file = new File("example.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// true = append file
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(23条)