1

我是 java 新手,我有一个在 txt 文件上写入信息的程序,我似乎唯一遇到的问题是,如果 fileWriter 正在附加文件,我无法编辑或重写文件上的特定行,是有什么方法可以将编写器设置为从文件开头而不是结尾开始而不擦除数据?因为这样我就无法编辑文件中的信息。提前感谢您的任何回答!

4

1 回答 1

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));
}
于 2013-07-04T14:18:06.517 回答