2015-04-06 32 views
2

我試圖在文件中打印132_000行。使用Java在文件中打印132000行

這裏是我的代碼:

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,3169行或1,31721行。

但在控制檯中,我能夠看到所有1,320,000個印刷品。

請讓我知道如果我在這裏做錯了什麼。

在此先感謝。

+4

嘗試退出前沖洗緩衝的作家:) – folkol 2015-04-06 11:26:28

回答

5

結束寫入剩餘數據

bw.close()。您可以使用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(); 
    } 
} 
3

關閉Writer,在您不關閉您的Writer程序