java中文件追加,如何在Java中将文本追加到现有文件?

I need to append text repeatedly to an existing file in Java. How do I do that?

解决方案

Are you doing this for logging purposes? If so there are several libraries for this. Two of the most popular are Log4j and Logback.

Java 7+

If you just need to do this one time, the Files class makes this easy:

try {

Files.write(Paths.get("myfile.txt"), "the text".getBytes(), StandardOpenOption.APPEND);

}catch (IOException e) {

//exception handling left as an exercise for the reader

}

Careful: The above approach will throw a NoSuchFileException if the file does not already exist. It also does not append a newline automatically (which you often want when appending to a text file). Steve Chambers's answer covers how you could do this with Files class.

However, if you will be writing to the same file many times, the above has to open and close the file on the disk many times, which is a slow operation. In this case, a buffered writer is better:

try(FileWriter fw = new FileWriter("myfile.txt", true);

BufferedWriter bw = new BufferedWriter(fw);

PrintWriter out = new PrintWriter(bw))

{

out.println("the text");

//more code

out.println("more text");

//more code

} catch (IOException e) {

//exception handling left as an exercise for the reader

}

Notes:

The second parameter to the FileWriter constructor will tell it to append to the file, rather than writing a new file. (If the file does not exist, it will be created.)

Using a BufferedWriter is recommended for an expensive writer (such as FileWriter).

Using a PrintWriter gives you access to println syntax that you're probably used to from System.out.

But the BufferedWriter and PrintWriter wrappers are not strictly necessary.

Older Java

try {

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));

out.println("the text");

out.close();

} catch (IOException e) {

//exception handling left as an exercise for the reader

}

Exception Handling

If you need robust exception handling for older Java, it gets very verbose:

FileWriter fw = null;

BufferedWriter bw = null;

PrintWriter out = null;

try {

fw = new FileWriter("myfile.txt", true);

bw = new BufferedWriter(fw);

out = new PrintWriter(bw);

out.println("the text");

out.close();

} catch (IOException e) {

//exception handling left as an exercise for the reader

}

finally {

try {

if(out != null)

out.close();

} catch (IOException e) {

//exception handling left as an exercise for the reader

}

try {

if(bw != null)

bw.close();

} catch (IOException e) {

//exception handling left as an exercise for the reader

}

try {

if(fw != null)

fw.close();

} catch (IOException e) {

//exception handling left as an exercise for the reader

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值