我是java新手,我有一个在txt文件上写信息的程序,唯一的问题似乎是,如果fileWriter附加了我不能编辑的文件,或者重写了文件上的特定行,有没有办法将写入器设置为从文件的开头开始,而不是在不擦除数据的情况下从结尾开始?因为这样我就不能编辑文件中的信息了。提前感谢您的回复!
发布于 2013-07-04 22:18:07
FileWriter类有几个构造函数。其中一些带有类型为boolean的"append“参数。您是否在true中使用了这些构造函数之一?
您应该对该参数使用false。
/**
* Constructs a FileWriter object given a File object. If the second
* argument is <code>true</code>, then bytes will be written to the end
* of the file rather than the beginning.
*
* @param file a File object to write to
* @param append if <code>true</code>, then bytes will be written
* to the end of the file rather than the beginning
* @throws IOException if the file exists but is a directory rather than
* a regular file, does not exist but cannot be created,
* or cannot be opened for any other reason
* @since 1.4
*/
public FileWriter(File file, boolean append) throws IOException {
super(new FileOutputStream(file, append));
}https://stackoverflow.com/questions/17472391
复制相似问题