用fileWriter重写一行

时间:2013-07-04 14:06:03

标签: java filewriter

我是新手在java中我有一个程序在txt文件上写入信息,我似乎唯一的问题是如果fileWriter附加了我无法编辑的文件,或者重写了一个特定的行文件,有没有办法设置编写器在文件的开头而不是结束而不删除数据?因为这样我就无法编辑文件中的信息。 提前感谢您的回答!

1 个答案:

答案 0 :(得分:0)

FileWriter类有几个构造函数。其中一些人用"追加" boolean类型的参数。你使用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));
}