2014-02-13 75 views
0

這裏一個空文件是我剛纔的問題的編輯版本:沒有任何內容

因此,這裏是我的目的:在一個.txt文件(HTML文件)中讀取,並把所需要的內容放到一個.txt文件。現在

,這個HTML文件中包含噸表格和格式的,我不需要,我只需要內容

import java.io.*; 


public class File { 


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

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

     String inputLine = ""; 
     int i=0; 

     while ((inputLine = in.readLine()) != null) { 
     if (inputLine.contains("Windows")) { 
     out.append(inputLine); 
     out.newLine(); 


     } 

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

它使一個名爲「output.txt的」文件,但它完全是空的。

它如何精確地排序字符串?是逐字逐句還是逐句句子?

以下是該文件的示例。 (有點)

<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> 

它可以執行它只是作爲Microsoft(R)Windows(R)服務器企業版,60嗎?

UPDATE:

它還執行 「喜」

+0

你在哪裏閱讀.txt文件。我所看到的僅僅是桌面的路徑,而不是實際的文件。 – Jimmy

+0

那這不是文件?我想如果你定義一個路徑,它會讀取文件 – ToxicGlow

回答

2

BufferedWriter不能用字符串參數進行實例化。用這個代替:

BufferedWriter out = new BufferedWriter(new FileWriter(output)); 

另外,還有一些在代碼中的一些錯誤:

  1. 的[]
  2. 它是將其設置爲前讀取輸入文件三次尚未初始化數組服務器服務器[]

它可以被簡化爲這樣:

 try { 
      String input= "D:\\input.txt"; 
      BufferedReader in = new BufferedReader(new FileReader(input)); 

      String output = "D:\\output.txt"; 
      BufferedWriter out = new BufferedWriter(new FileWriter(output)); 

      String inputLine = ""; 

      while ((inputLine = in.readLine()) != null) { 
       if (inputLine.contains("Windows")) { 
        out.append(inputLine); 
        out.newLine(); 
       } 
      } 

      in.close(); 
      out.flush(); 
      out.close(); 
     } catch (Exception e) { 
     } 
+0

它使一個空文件,它沒有內容,我該怎麼辦? – ToxicGlow

+0

在你的情況下,比較字符串是「Windows」而不是「windows」。所以,你應該修改這個行:inputLine.contains(「windows」)到inputLine.contains(「Windows」)。沒關係,我會自己做。請參閱更新的代碼。 –

0

這是編譯時錯誤,而不是運行時錯誤?如果是這樣,那可能是因爲BufferedWriter沒有一個構造函數,它只接受一個字符串,而只有另一個Writer。相反,嘗試:

String output="output.txt"; 
BufferedWriter out = new BufferedWriter(new FileWriter(output)); 

BufferedWriter將被設計爲周圍的其他作家類包裝,以防止各輸入字節被直接寫入到底層輸出流。

0

也嘗試實例化你的數組!你有String Workstation[];String Server[];,但是在這種狀態下它們是空的引用指向無處,並且會在修復編譯錯誤後給你nullpointer異常。

嘗試初始化它們,如String Server[] = new String[sizeOfArray];或考慮使用List<String>代替。

相關問題