使用Java在文件中打印132000行

ISG*_*ind 2 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();

        }

    }

}
Run Code Online (Sandbox Code Playgroud)

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

但在控制台我能看到所有1,32,000印刷品.

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

提前致谢.

Ell*_*sch 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

而第二个(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();
    }
}
Run Code Online (Sandbox Code Playgroud)