Using FileWriter and BufferedWriter clearing file for some reason?

妖精的绣舞 提交于 2019-12-23 18:16:08

问题


For some reason, when I create a new BufferedWriter and FileWriter in my program (Even if I haven't used it to write anything yet), it clears the file I have selected of all it's text.

selectedFile is determined by a JFileChooser.

public static File selectedFile;

    public static void Encrypt() throws Exception {

    try {
        //if I comment these two writers out the file is not cleared.
        FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);

        List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
                Charset.defaultCharset());
        for (String line : lines) {
            System.out.println(line);
            System.out.println(AESencrp.encrypt(line));

            /*file is cleared regardless of whether or not these are commented out or
             * not, as long as I create the new FileWriter and BufferedWriter the file
             * is cleared regardless.*/

            //bw.write(AESencrp.encrypt(line));
            //bw.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

AESencrp.encrypt is just an encrypting class I have, it won't affect it. If I create a new FileWriter and BufferedWriter then this loop isn't even run (At least I do not believe so, as I don't get the encryption of line or the original contents of the file printed, which prints if I haven't created the new FileWriter/BufferedWriter.)

        for (String line : lines) {
            System.out.println(line);
            System.out.println(AESencrp.encrypt(line));

            /*file is cleared regardless of whether or not these are commented out or
             * not, as long as I create the new FileWriter and BufferedWriter the file
             * is cleared regardless.*/

            //bw.write(AESencrp.encrypt(line));
            //bw.close();
        }

回答1:


This is because the constructor of FileWriter you are using truncates the file if it already exists.

If you want to append the data instead, use:

new FileWriter(theFile, true);



回答2:


It sounds like you want to append to the file, and not overwrite it. Use the proper FileWriter constructor that takes a boolean on whether to append.

FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile(), true);

The constructor you are using, without a boolean, defaults to "overwrite" mode. As soon as you create the FileWriter in overwrite mode, it clears the file so it can start writing from the beginning.

Passing true as the second argument will allow you to append instead of overwrite.




回答3:


you open it for write (for appending use the constructor with append option). what do you expect what else should happen? Further your outcommented close() is at the wrong place. it should be outside the loop.



来源:https://stackoverflow.com/questions/17244713/using-filewriter-and-bufferedwriter-clearing-file-for-some-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!