2014-02-14 51 views
0

只是爲了您的信息:我有與此代碼有關的另一個問題,我已經問過它,我編輯了這個問題(完全)的問題,但我沒有得到任何答覆。我的FileWriter文件是完全空的

目的:我試圖讀取文件(包含HTML的.txt)並對內容進行排序。它確實創建了一個文本文件,並且它完全是空的。 我讀了一些類似的情況,但我看到他們的錯誤沒有添加out.flush()或out.close()文件。

這裏是代碼至今:

import java.io.*; 


public class File { 

    public static void main(String[] args) throws IOException { 
     try { 
      String input = "SCCM.txt"; 
      BufferedReader in = new BufferedReader(new FileReader(input)); 
      String output = "output.txt"; 
      BufferedWriter out = new BufferedWriter(new FileWriter(output)); 

      String inputLine = "", s="windows"; 
      String regex = "&nbsp"; 
      while ((inputLine = in.readLine()) != null) { 
       if (inputLine.contains(s)) { 
        inputLine.split(regex); 
        out.append(inputLine); 
        out.newLine(); 
       } 
       in.close(); 
       out.flush(); 
       out.close(); 
      } 
     } 
     catch(IOException e) { 
      System.out.println("Hi"); 
     } 
    } 
} 

內容我想排序:

<TR class="RowDark"> 
    <TD width=0><A href="Report.asp?ReportID=100&amp;sp=Service+Pack+1&amp;os=Microsoft%28R%29+Windows%28R%29+Server+2003%2C+Enterprise+Edition"><IMG border=0 src="images/icolink3.gif" alt="Open the target" width=11 height=11></A></TD> 
    <TD class=SimpleTextSmall>&nbsp;Microsoft(R)&nbspWindows(R)&nbspServer&nbsp2003,&nbspEnterprise&nbspEdition&nbsp;</TD> 
    <TD class=SimpleTextSmall>&nbsp;Service&nbspPack&nbsp1&nbsp;</TD> 
    <TD class=SimpleTextSmall>&nbsp;60&nbsp;</TD> 
</TR> 

輸出我想: 微軟(R)的Windows(R)Server 2003企業版, Service Pack 1,60

我讀的東西(供您參考): Java txt File from FileWriter is empty

UPDATE:

import java.io.*; 

公共類文件{

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

    String input = "SCCM.txt"; 
    BufferedReader in = new BufferedReader(new FileReader(input)); 
    String output = "output.txt"; 
    BufferedWriter out = new BufferedWriter(new FileWriter(output)); 


    try { 

     String inputLine = "", s="Windows"; 
     String regex = "&nbsp"; 
     while ((inputLine = in.readLine()) != null) { 
      if (inputLine.contains(s)) { 
       inputLine.split(regex); 
       out.write(inputLine); 
       out.newLine(); 
      } 

     } 
    } 
    catch(IOException e) { 
     System.out.println("Hi"); 

     in.close(); 
     out.flush(); 
     out.close(); 
    } 
} 

}

UPDATE:

文件不輸出(噸愛情的那些誰幫助)

這是怎麼回事

<TD width=0><A href="Report.asp?ReportID=100&amp;sp=Service+Pack+1&amp;os=Microsoft%28R%29+Windows%28R%29+Server+2003%2C+Enterprise+Edition"><IMG border=0 src="images/icolink3.gif" alt="Open the target" width=11 height=11></A></TD> 
      <TD class=SimpleTextSmall>&nbsp;Microsoft(R)&nbspWindows(R)&nbspServer&nbsp2003,&nbspEnterprise&nbspEdition&nbsp;</TD> 

任何提示/提示/技巧,可能使其可讀?

+0

另外要注意,'String.contains()'是大小寫敏感的。您的搜索字符串是「窗口」,但您搜索的行包含「Windows」。 – csmckelvey

+0

我嘗試了兩個提供的建議,但文件仍然是空的 – ToxicGlow

+0

您現在搜索了「Windows」(大寫)_and_將while()語句移出while循環嗎? – csmckelvey

回答

2

您正在關閉while循環內的輸出流,因此幾乎立即拋出IOException,但不會顯示異常的詳細信息。這些陳述

in.close(); 
out.flush(); 
out.close(); 

應在finally塊

try { 
    ... 
catch (IOException e) { 
    e.printStackTrace(); // add me 
} finally { 
    in.close(); 
    out.flush(); 
    out.close(); 
} 

只有這樣,你可以檢查字符串Windows(如前所述in the comments

String s = "Windows"; 
+0

好奇心,你是如何鏈接到這樣的具體評論? – csmckelvey

+0

@Takendarkk鏈接可以在後的時間找到,例如8分鐘前 – Reimeus

+0

啊,很酷的東西。謝謝。 – csmckelvey

0

inputLine決不會包含windows但是它確實包含Windows,這會導致條件爲始終評估爲false,從而阻止追加方法d被叫。

更改以下行:

String inputLine = "", s="windows"; 

String inputLine = "", s="Windows"; 

或小寫s比較之前:

+0

我試過你的建議,謝謝:)但仍然我的文件是空的 – ToxicGlow