Java 覆盖现有的输出文件 [英] Java overwriting an existing output file

查看:42
本文介绍了Java 覆盖现有的输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序目前正在使用

FileOutputStream output = new FileOutputStream("output", true);

while 循环会在尚未创建的情况下创建输出文件,并使用

A while loop creates the output file if it is not yet created and appends some data to this file for every iteration of the while loop using

 output.write(data).  

这很好,正是我想要的.

This is fine and is what I want.

如果我再次运行该程序,文件的大小只会增加一倍,因为它将确切信息附加到文件末尾.这不是我想要的.如果我再次运行该程序,我想覆盖该文件.

If I run the program again the file just doubles in size as it appends the exact information to the end of the file. This is not what I want. I would like to overwrite the file if I run the program again.

推荐答案

本文档 表明您传递的参数是 append 参数.

This documentation suggests that the parameter you're passing is the append parameter.

签名如下

FileOutputStream(File file, boolean append)

您应该将该参数设置为 false,因为您不想追加.

You should set that parameter to false since you don't want to append.

FileOutputStream output = new FileOutputStream("output", false);

这篇关于Java 覆盖现有的输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆