本文介绍了FileWriter不写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

        try {
            f1 = new File("sink.txt");
            f1.createNewFile();
            fw = new FileWriter(f1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ... code ...
        System.out.println(sequence);
        System.out.println(mySink.indexOf(sequence));
        String result = "";
        int firstIndex = mySink.indexOf(sequence);
        if (firstIndex >= 0) {
            System.out.println(true);
            int secondIndex = mySink.indexOf(sequence, firstIndex + sequence.length());
            if (secondIndex >= 0) {
                System.out.println(true);
                result = mySink.substring(firstIndex, secondIndex + sequence.length());
                System.out.println(result);
            }
        }
        try { // Write it to file
            fw.write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("done");

当我跑掉时,它会打印字符串 sequence mySink 里面的索引。然后它进入if语句并打印出两个 true 并打印出结果所以我知道结果已成功初始化。但当我查看文件 sink.txt 时,我发现它是空白的。为什么这样做?我在代码中遗漏了什么吗?之前它正在工作,我添加了一些代码,它就是这样做的。在执行程序期间,我从未触及 FileWriter 文件。提前致谢!

When I ranit, it printed the string sequence and the index of it inside mySink. Then it goes inside the if statements and printed out the two true and printed out result so I know result was initialized successfully. But when I look into the file sink.txt, I see that it's blank. Why is it behaving this way? Did I miss something in my code? It was working before and I added some more code and it does this. I never touch the FileWriter or the File during the execution of the program. Thanks in advance!

如果你想看到这是我的输出:

This is my output if you want to see:

[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 40, 98, 48, 43, 47, 89]
2000466
true
true
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 40, 98, 48, 43, 47, 89]
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 40, 59, 48, 43, 47, 89]
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 81, 59, 48, 43, 47, 89]
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 81, 98, 48, 43, 47, 89]
[93, 73, 74, 81, 89, 70, 50, 80, 51, 83, 62, 13, 50, 0, 40, 98, 48, 43, 47, 89]
done


推荐答案

简短的回答是你没有关闭(或刷新)你的 FileWriter 。这意味着您的应用程序将退出,而未写入的数据仍然位于文件缓冲区中。

The short answer is that you are not closing (or flushing) your FileWriter. That means that you application will exit with unwritten data still sitting in the file buffers.

您的代码中还有许多其他错误。从顶部开始:

There are a number of other mistakes in your code. Starting from the top:

    try {
        f1 = new File("sink.txt");
        f1.createNewFile();
        fw = new FileWriter(f1);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }




  1. createNewFile 调用是多余的。以下新的FileWriter 将创建该文件。

  1. The createNewFile call is redundant. The following new FileWriter will create the file.

您正在捕获异常并继续,就好像什么都没有发生了。你>>不能<<继续这些例外。如果您成功打开文件,其余代码只能正常工作。

You are catching exceptions and continuing as if nothing has happened. You >>cannot<< continue from those exceptions. The rest of your code can only work properly if you succeeded in opening the file.

您不需要捕获 FileNotFoundException 除非您打算以不同方式处理它。捕获 IOException 就足够了,因为它是前者的超类。

You don't need to catch FileNotFoundException unless you intend to handle it differently. Catching IOException is sufficient, because it is a superclass of the former.

此时,你使用try-with-resources:

At this point, you should be using try-with-resources:

f1 = new File("sink.txt");
try (FileWriter fw = new FileWriter(f1)) {

   // compute stuff

   // write stuff to file

} catch (FileNotFoundException ex) {
    System.out.println(ex.getMessage());
} catch (IOException ex) {
    // This is ugly for a real app.  However, an IOException that
    // is not a FileNotFoundException is "unexpected" at this point
    // and providing a user-friendly explanation would be tricky.
    ex.printStackTrace();
}

try-with-resources将导致 fw 在块退出时自动关闭。关闭作者将首先冲洗它。

The try-with-resources will cause fw to be closed automatically when the block exits. Closing the writer will first flush it.

这篇关于FileWriter不写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:32