使用Java在文件中打印132000行

时间:2015-04-06 11:19:37

标签: java file-io

我正在尝试在文件中打印132_000行。

这是我的代码:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Random;
import java.util.Scanner;

public class CredentialTemplate {

    public static void main(String[] args) throws IOException {

        // Declaring output file
        File fout = new File(
                "D:\\testout.txt");
        FileOutputStream fos = new FileOutputStream(fout);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

        int start = 0, stop = 0;
        Scanner x = new Scanner(System.in);
        System.out.print("Start:  ");
        start = x.nextInt();
        System.out.print("End:  ");
        stop = x.nextInt();

        Random r = new Random();

        for (int i = start; i <= stop; i++) {

            System.out.println("Importeduser" + i + ",Test,4," + r.nextInt(9)
                    + r.nextInt(9) + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)+ r.nextInt(9)+",0,"
                    + r.nextInt(9) + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                    + r.nextInt(9) + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                    + r.nextInt(9) + r.nextInt(9));
            bw.write("Importeduser" + i + ",Test,4," + r.nextInt(9)
                    + r.nextInt(9) + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)+ r.nextInt(9)+",0,"
                    + r.nextInt(9) + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                    + r.nextInt(9) + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                    + r.nextInt(9) + r.nextInt(9));
            bw.newLine();

        }

    }

}

我面临的问题: 我没有在txt文件中获得所有1,32,000行。 有时它的1,31,693行或1,31,721行。

但在控制台中,我能够看到所有打印的1,32,000张。

如果我在这里做错了,请告诉我。

提前致谢。

2 个答案:

答案 0 :(得分:5)

您没有关闭Writer。您可以使用finally块,也可以使用try-with-resources。第一个看起来像,

try {
    for (int i = start; i <= stop; i++) {
        String line = "Importeduser" + i + ",Test,4," + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + ",0," + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + r.nextInt(9);
        System.out.println(line);
        bw.write(line);
        bw.newLine();
    }
} finally {
    if (bw != null) {
        bw.close();
    }
    if (fos != null) {
        fos.close();
    }
}

第二个(try-with-resources)可能看起来像

try (FileOutputStream fos = new FileOutputStream(fout);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                fos))) {
    for (int i = start; i <= stop; i++) {
        String line = "Importeduser" + i + ",Test,4," + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + ",0," + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + r.nextInt(9)
                + r.nextInt(9) + r.nextInt(9) + r.nextInt(9);
        System.out.println(line);
        bw.write(line);
        bw.newLine();
    }
}

答案 1 :(得分:3)

关闭您的编写器以编写剩余数据

在程序结束时执行bw.close()