2016-03-20 50 views
2

試圖僅獲取包含1個單詞的行。只提取一個單詞的行?

當前方法得到正確的結果,但有時輸入文件在每個單詞之間有超過4行。所以需要一種方法來獲得只有包含1個單詞的行。任何想法?

這裏是什麼樣的輸入文本看起來像一個例子:

adversary 
someone who offers opposition 
The students are united by shared suffering, and by a common adversary. 
— New York Times (Nov 10, 2014) 
aplomb 
great coolness and composure under strain 
I wish I had handled it with aplomb. 
— New York Times (May 18, 2014) 
apprehensive 

所以輸出應該是這樣的:

adversary 
aplomb 
apprehensive 

這是迄今爲止代碼:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.nio.file.Files; 
import java.nio.file.Paths; 

public class Process { 

    public static void main(String[] args) { 

     String fileNameOutput = "OutputFile.txt"; 
     String fileName = "InputWords"; 

     try (BufferedReader bReader = Files.newBufferedReader(Paths.get(fileName))){ 

      PrintWriter outputStream = new PrintWriter(fileNameOutput); 
      int lineNum = 0; 
      String line = null; 

      while ((line = bReader.readLine()) != null) { 
       lineNum++; 

      if (lineNum % 4 == 0) continue; 


       outputStream.println(line); 


      } 
       outputStream.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 



    } 

} 

謝謝你的時間。


編輯

從下面的建議的修復充分利用控制檯此錯誤。

java.nio.charset.MalformedInputException: Input length = 1 
    at java.nio.charset.CoderResult.throwException(Unknown Source) 
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source) 
    at sun.nio.cs.StreamDecoder.read(Unknown Source) 
    at java.io.InputStreamReader.read(Unknown Source) 
    at java.io.BufferedReader.fill(Unknown Source) 
    at java.io.BufferedReader.readLine(Unknown Source) 
    at java.io.BufferedReader.readLine(Unknown Source) 
    at Process.main(Process.java:20) 

回答

2

取決於你的定義

  • 字母
  • 的任何一個步驟的順序: 「詞」 的不是空白的字符
  • 表示單詞的字形(例如, )

讓我們堅持前兩個,並使用正則表達式來檢查,所以我們可以很容易地忽略前導空白和尾隨空白。這裏有三種方式:

if (line.matches("\\s*[a-zA-Z]+\\s*")) // One or more ASCII letters 
    outputStream.println(line); 
if (line.matches("\\s*\\p{L}+\\s*")) // One or more Unicode letters 
    outputStream.println(line); 
if (line.matches("\\s*\\S+\\s*")) // One or more non-space characters 
    outputStream.println(line); 

至於MalformedInputException,它是由代碼頁不匹配引起的(異常被拋出StreamDecoder)。

newBufferedReader(path)以UTF-8讀取文件,該文件可能在系統默認代碼頁中,而不是在UTF-8中。

改爲使用newBufferedReader(path, Charset.defaultCharset())

+0

謝謝!我給了這個嘗試,但仍然得到描述中的錯誤...任何想法,爲什麼? –

+0

是的,['newBufferedReader()'](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#newBufferedReader-java.nio.file.Path-)以UTF-8讀取文件,該文件可能不是UTF-8,而是系統默認的代碼頁。改爲使用'newBufferedReader(path,Charset.defaultCharset())'。 – Andreas

+0

但是,沒有任何人在這裏建議任何編輯的代碼工作正常。所以這不可能是問題。 –

3

好,而不是

if (lineNum % 4 == 0) continue; 

條件,你可以簡單地檢查你剛纔讀的行是否包含不止一個令牌:

if (line.split(" ").length > 1) continue; 

if (line.indexOf(" ") >= 0) continue; 

後一種情況應該比前者更有效率。

+0

啊,真棒,現在就試試。乾杯! –

+0

是啊給出了一個錯誤...(MalformedInputException:輸入長度= 1) –

+0

@JonathanLaliberte什麼行代碼給出了這個異常? – Eran

1

取而代之的

if (lineNum % 4 == 0) continue; 

只需選中行包含空間。

if(line.trim().contains(" ")) continue; 
+0

「該方法包含(CharSequence)在String類型中不適用於參數(char)」 –

+0

@JonathanLaliberte剛剛編​​輯了我的答案。 –

+0

嗯謝謝,但仍然得到這個該死的錯誤出於某種原因,即使這個建議。有任何想法嗎? –

1

你得到一個錯誤的java.io.BufferedReader.readLine(來源不明)這麼說是沒有找到輸入文件... 嘗試更改文件名

String fileName = "InputWords"; 

to 

String fileName = "InputWords.txt"; 
+0

nah無關。已經檢查過 –

1

工作!需要添加字符集。

public static void main(String args[]){ 
     //testAnimal(); 
     String fileNameOutput = "OutputFile.txt"; 
      String fileName = "InputWords.txt"; 

      Charset cs = Charset.defaultCharset() ; 
      try (BufferedReader bReader = Files.newBufferedReader(Paths.get(fileName), cs)){ 

       PrintWriter outputStream = new PrintWriter(fileNameOutput); 
       int lineNum = 0; 
       String line = null; 

       while ((line = bReader.readLine()) != null) { 
        lineNum++; 

        if (line.split(" ").length > 1) continue; 


        outputStream.println(line); 


       } 
        outputStream.close(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 


    } 
+0

謝謝你,先生。現在最後工作。乾杯 –

相關問題