2016-11-22 79 views
1

問題:我無法用空格解析我的文件test.txt。我可以1)讀取文本文件,並且我可以2)解析字符串,但是我無法連接這兩個文件並解析文本文件!我的目的是學習如何分析文本文件。這是一個簡化的方法。合併讀取和解析文本text.txt

進度:到目前爲止,我可以使用FileReader和BufferedReader讀取test.txt,並將其打印到控制檯。此外,我可以解析簡單的字符串變量。單個操作運行,但我正在努力解析一個實際的文本文件。我相信這是因爲我的test.txt存儲在緩衝區中,並且在.close()之後,我無法打印它。

文本文件的內容: 這是創建 文本文件,只 用於測試目的。

代碼:

import java.io.*; 

public class ReadFile { 

    //create method to split text file, call this from main 
    public void splitIt(String toTest) 
    { 
     String[] result = toTest.split(" "); 

     for (String piece:result) 
     { 
      //loop through the array and print each piece 
      System.out.print(piece); 
     } 
    } 


    public static void main(String[] args) { 

     //create readfile method  
     try 
     { 
      File test = new File("C:\\final\\test.txt"); 
      FileReader fileReader = new FileReader(test); 
      BufferedReader reader = new BufferedReader(fileReader); 

      String line = null; 

      //While there are still lines to be read, read and print them 
      while((line = reader.readLine()) != null) 
      { 
       System.out.println(line); 
       splitIt(line); 
      } 

      reader.close(); 
     } 

     //Catch those errors! 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 

//  readFileMethod a = new readFileMethod(line); 
     System.out.println(a.splitIt()); 

    } 

} 

搶佔感謝您的分享你的知識。閱讀和解析的許多帖子都已經在這裏解決了,但我沒有理解實現他人的解決方案。請原諒,我只是在幾個月的時間裏一直在學習Java,並仍然在與基礎知識鬥爭。

+0

你能解釋一下你試圖實現的內容嗎? – root

+0

嘿根,我已經添加了文本文件內容(只是簡單的佔位符文本),並描述了我的目的。我想讓文本文檔中的每個單詞用行分隔。 – PizzaAndCode

回答

0

大廈可怕的袋熊和你的代碼,我做了一些修改調用它。 它現在應該打印正在讀入的行和每個由空格分隔的單詞。

import java.io.*; 

public class ReadFile { 

    //create method to split text file, call this from main 
    public static void splitIt(String toTest) 
    { 
     String[] result = toTest.split(" "); 

     for (String piece:result) 
     { 
      //loop through the array and print each piece 
      System.out.println(piece); 

     } 
    } 


    public static void main(String[] args) { 

     //create readfile method 
     try 
     { 
      File test = new File("C:\\final\\test.txt"); 
      FileReader fileReader = new FileReader(test); 
      BufferedReader reader = new BufferedReader(fileReader); 

      String line = null; 

      //While there are still lines to be read, read and print them 
      while((line = reader.readLine()) != null) 
      { 
       System.out.println(line); // print the current line 
       splitIt(line); 
      } 

      reader.close(); 
     } 

     //Catch those errors! 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 

    } 

} 
+0

這工作完美無瑕,我感謝完整的工作代碼:) – PizzaAndCode

+1

@ ScaryWombat erm ...我真的很欣賞你的答案。爲什麼要提供完整的解決方案downvote root?關於諷刺性的學習評論,自從發佈這個問題以來,我已經閱讀了幾百頁Java,並大大增加了我的理解。因此,我從頭開始重寫了這些代碼幾次。我不知道你爲什麼會對曾經和你站在同一個鞋子裏的人採取如此便宜的槍擊......但這並不重要。儘管如此,我很欣賞這個學習者和老師的社區。 – PizzaAndCode

3

確定可以使分裂成mthod

private static void splitIt (String toTest) { 

    String[] result = toTest.split(" "); 

    for (String piece:result) 
    { 
     //loop through the array and print each piece. 
     System.out.println(piece); 
    } 
} 

那麼你可以從內部

while((line = reader.readLine()) != null) 
    { 
     System.out.println(line); 
     splitIt (line); 
    } 
+0

嗨嚇人的袋熊,我*試圖*將您的答案納入我的代碼塊,但我仍然受到事實,我從靜態主要內部調用非靜態(我相信)的阻礙。幾小時的工作和30頁的閱讀沒有產生任何解決方案,但很明顯,這是一個常見的初學者問題:(你有任何線索? – PizzaAndCode

+0

@PizzaAndCode由於'splitIt'不是一個靜態方法,它需要它包含的類首先被實例化(創建一個新的類對象),你可以在main中創建一個新的ReadFile對象,然後從該對象本身調用該方法,但它可能是最簡單的case)更有意義)只是使'splitIt'成爲一個靜態方法 – kunruh

+0

@kunruh感謝您的評論我編輯了我的答案,使這種方法'靜態' –