如何将文本输出写入文件?

时间:2014-03-12 16:56:44

标签: java io

在参考本网站上的一些问题后,我了解到BufferedWriter可用于将文本内容写入文件。我使用了以下代码行。但是,我看到没有输出写入文件。该文件为空。

被修改

  

代码段:

private void findPrimes(int min, int max) throws IOException {
        for (int i = min; i <= max; i++) {
            boolean isPrime = true;
            BufferedWriter writer = new BufferedWriter(new FileWriter("/home/hduser/prime.txt"));
            if (i == 1) {
                writer.write("1 is not a prime.");
                continue;
            }
            if (i == 2 || i == 3 || i == 5 || i == 7) {
                writer.write(i + " is a prime");
                continue;
            }
            for (int j = 2; j <= 9; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                writer.write(i + " is a prime.");
            } else {
                writer.write(i + " is not a prime. Its factors are ");
                writer.newLine();
                writer.flush();
                writer.close();
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

除了if条件之外,就我而言,Writer接口不能保证在您明确关闭数据之前将数据刷新到磁盘。

尝试

writer.close()

当您完成文件编写并查看此问题是否仍然存在时。

修改

您应该在方法开头只创建一次BufferedWriter,因为默认文件写入过程包括截断以前的文件内容。您的代码应该类似于

private void findPrimes(int min, int max) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter("/home/hduser/prime.txt"));

    for (int i = min; i <= max; i++) {
        boolean isPrime = true;

        if (i == 1) {
            writer.write("1 is not a prime.");
            writer.flush();
            continue;
        }

        if (i == 2 || i == 3 || i == 5 || i == 7) {
            writer.write(i + " is a prime");
            writer.flush();
            continue;
        }

        for (int j = 2; j <= 9; j++) {
            if (i % j == 0) {
                isPrime = false;
                break;
            }
        }

        if (isPrime) {
            writer.write(i + " is a prime.");
            writer.flush();
        } else {
            writer.write(i + " is not a prime. Its factors are ");
            findFactors(i);
            writer.newLine();
            writer.flush();
        }
    }
    writer.close();
}

答案 1 :(得分:0)

你有没有试过

writer.flush();

之前打电话

writer.close();

答案 2 :(得分:0)

这将写入文件检查..

 private static void findPrimes(int min, int max) throws IOException {

   BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/home/hduser/prime.txt")));

for (int i = min; i <= max; i++) {
    boolean isPrime = true;
     if (i == 1) {
        writer.write("1 is not a prime.");
        continue;
    }
    if (i == 2 || i == 3 || i == 5 || i == 7) {
        writer.write(i + " is a prime");

        continue;
    }
    for (int j = 2; j <= 9; j++) {
        if (i % j == 0) {
            isPrime = false;
            break;
        }
    }
    if (isPrime) {

        writer.write(i + " is a prime.");
        writer.newLine();

    } else {
        writer.newLine();
        writer.write(i + " is not a prime.");

    }

}

writer.flush();

}