2013-04-20 238 views
0

我有一個程序,我在其中使用while循環打印到控制檯和html文件。但是,即使它打印到控制檯,該程序也只會將最後一行打印到html文件。這裏是一個應該打印從文本文件中提取的線9中的信息16雖然循環只打印一次而不是整個循環

//read the txt file 
Path objPath = Paths.get("dirsize.txt"); 
if (Files.exists(objPath)) 
{ 

    File objFile = objPath.toFile(); 
    try(BufferedReader in = new BufferedReader(
          new FileReader(objFile))) 
    { 
    String line = in.readLine(); 
    int lineNum = 1; 
    //extract the month 
    String monthSubstring = line.substring(25,27); 
    month = Integer.parseInt(monthSubstring) -1 ; 
    //extact the year 
    String yearSubstring = line.substring(31,35); 
    year = (Integer.parseInt(yearSubstring)); 
    //extract the date 
    String daySubstring = line.substring(28,30); 
    day = Integer.parseInt(daySubstring); 
    PrintWriter out = new PrintWriter (new BufferedWriter(new FileWriter("Output.html"))); 
    while(line != null) 
    { 
     line = in.readLine(); 
     lineNum ++; 
     if (lineNum == 3) 
     { 
      //extract the hour 
      String hourSubstring = line.substring(21,23); 
      hour = Integer.parseInt(hourSubstring); 
      //extract the minute 
      String minuteSubstring = line.substring(24,26); 
      minute = Integer.parseInt(minuteSubstring); 
      //extract the second 
      String secondSubstring = line.substring(27,29); 
      second= Integer.parseInt(secondSubstring); 
      } 
     if(lineNum ==5) 
     { 
      //Extract the branch 
      strBranch = line.substring(22, 27);   
     }   
     if (lineNum == 6) 
     { 
      //Extract the computer 
      strCPU = line.substring(26,32); 
     } 
    if (lineNum >= 9 && lineNum <= 16) 
     { 
      //call the MyDirectory methods to get the files name,size and number of files 
      MyDirectory directory = new MyDirectory(line); 
      fileName = directory.getName(); 
      fileSize = directory.getsize(); 
      fileNum = directory.getNum(); 
      //create the dteDate calender 
      GregorianCalendar dteDate = new GregorianCalendar(year, month, day, hour, minute, second); 
      //fromat the date 
      SimpleDateFormat strDate = new SimpleDateFormat("MMM dd, yyyy h:m"); 
     //Write the data to the file 
     out.println((strBranch + "; " + 
        strCPU + "; " + 
        strDate.format(dteDate.getTime())) + "; " + 
        fileName + "; " + 
        fileSize + "; " + 
        fileNum); 
     //create the necessary output for the console 
      System.out.println((strBranch + "; " + 
        strCPU + "; " + 
        strDate.format(dteDate.getTime())) + "; " + 
        fileName + "; " + 
        fileSize + "; " + 
        fileNum); 
     }  
     out.flush(); 
    } 

    } 

    //catch any IOExceptions and print out e 
catch (IOException e) 
    { 
    System.out.println(e); 
    } 

} 

編輯我的循環代碼:通過諮詢在發佈的答案我現在有我的一切,但有即時輸出一個困難的時間格式化它的HTML文件具有所有的行沒有間距連在一起我試着用「\ n」在我的輸出結束,但然後我的代碼不編譯,因爲它說null不允許有任何人知道如何當它不會讓我把「\ n」放在我的打印方法的末尾時,將其格式化爲行?

我不確定這是否代碼太多或不足以解決問題,如果我應該添加任何東西到我的文章或拿出任何東西只是讓我知道。

回答

3

這是相當明顯的 - 創建文件每次while循環迭代(見下文):

   PrintWriter out = new PrintWriter (
          new BufferedWriter(
          new FileWriter("Output.html"))); 
     //Write the data to the file 
     out.println((strBranch + "; " + 
        strCPU + "; " + 
        strDate.format(dteDate.getTime())) + "; " + 
        fileName + "; " + 
        fileSize + "; " + 
        fileNum); 
     //flush the data and close the output stream 
     out.close(); 

進入while循環,而不是之前,您應該創建文件。

所以,你的代碼看起來應該像:

 PrintWriter out = new PrintWriter (new BufferedWriter(new FileWriter("Output.html"))); 
    while(line != null) 
    { 
     //perform your file operations here 
    } 
    out.close(); 

---- ----編輯

你更新了你的代碼,但它仍然不能確定。你關閉了文件第二循環內:

當下次這個循環迭代你還是試着寫信給你的文件(然後再次關閉它,即使它是在先前的迭代關閉)。

+0

也許我實現了這個錯誤,但它並沒有解決我的問題我更新我的代碼到我原來的帖子,所以你可以看到我是怎麼做到的 – 2013-04-21 00:01:52

+0

嗯現在我沒有得到任何東西在HTML文件它完全是空編輯我的代碼,以顯示我改變了什麼,因爲我可以做到這一點錯了 – 2013-04-21 00:12:02

+0

我把關閉操作後第二個}後循環結束,但現在我的HTML文件是空的,並沒有收到我的打印方法的任何數據 – 2013-04-21 00:18:48

0

在輸出文件中只看到一行,因爲每次寫入文件時都覆蓋該文件。

新的FileWriter( 「Output.html」,真正:

如果你需要創建而內的PrintWriter(LINENUM> = 9 & & LINENUM < = 16)循環,在追加模式打開文件);

將解決您的問題。

+0

這確實解決了不是所有的問題寫入文件但創建文件上的所有內容都是在一起而不是新行的問題。我試圖用out.println(打印的東西在這裏)的結尾處的「\ n」來解決這個問題,但是我得到了一個錯誤,那就是void類型是不允許的 – 2013-04-21 00:07:16

+0

實際上,你不需要在BufferedWriter中包裝PrintWriter, BufferedWriter單獨適用於你的情況。只需調用BufferedWriter對象上的write()方法,然後調用newLine()方法即可。這應該解決它。 – Suchet 2013-04-21 00:15:51

+0

我改變了我的構造函數爲BufferedWriter out = new BufferedWriter((new FileWriter(「Output.html」)));但現在程序無法找到out的println方法。println – 2013-04-21 00:25:12