2014-02-24 37 views
-5

如何使用java打印包含特定單詞的文件中的行?如何使用java打印包含特定單詞的文件中的行?

想要創建一個簡單的實用程序,該實用程序允許在文件中查找單詞並打印給定單詞所在的完整行。

我已經做了這麼多算的發生,但不會沒什麼兩樣鋤頭打印包含它的行...

 import java.io.*; 

     public class SearchThe { 
      public static void main(String args[]) 
      { 
      try 
       { 
       String stringSearch = "System"; 

       BufferedReader bf = new BufferedReader(new FileReader("d:/sh/test.txt")); 

     int linecount = 0; 
      String line; 

     System.out.println("Searching for " + stringSearch + " in file..."); 

     while ((line = bf.readLine()) != null) 
     { 
      linecount++; 
      int indexfound = line.indexOf(stringSearch); 

      if (indexfound > -1) 
      { 
       System.out.println("Word is at position " + indexfound + " on line " + linecount); 
      } 
     } 
     bf.close(); 
    } 
    catch (IOException e) 
    { 
     System.out.println("IO Error Occurred: " + e.toString()); 
    } 
} 

}

+3

您可以使用來自表示每一行的String實例的'contains'方法。如果你想避免錯誤的肯定匹配,你可以使用帶有字邊界的正則表達式。無論如何,請描述您的代碼存在哪些問題,以便我們可以幫助您解決問題。 – Pshemo

+3

你到現在爲止嘗試過什麼?一些代碼請... – Zeeshan

+0

你能夠逐行閱讀文件嗎?如果是,爲什麼沒有代碼存在? – Smutje

回答

4

假設你是從FILE1.TXT命名然後你可以用下面的代碼來打印包含特定單詞的所有行文件讀取。並且可以說你正在搜索單詞「foo」。

import java.util.*; 
import java.io.*; 
    public class Classname 
    { 
     public static void main(String args[]) 
     { 
     File file =new File("file1.txt"); 
     Scanner in = null; 
     try { 
      in = new Scanner(file); 
      while(in.hasNext()) 
      { 
       String line=in.nextLine(); 
       if(line.contains("foo")) 
        System.out.println(line); 
      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    }} 

希望這段代碼有幫助。

0

你需要做這樣的事情

public void readfile(){ 
    try { 

     BufferedReader br; 
     String line; 

     InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("file path"), "UTF-8"); 
     br = new BufferedReader(inputStreamReader); 
     while ((line = br.readLine()) != null) { 
      if (line.contains("the thing I'm looking for")) { 
      //do something 
      } 
      //or do this 
      if(line.matches("some regular expression")){ 
      //do something 
      } 
     } 

     // Done with the file 
     br.close(); 
     br = null; 

     } 
     catch (Exception ex) { 
     ex.printStackTrace(); 
     } 
    } 
+0

1.不要吞噬例外 2.你的代碼可以留下未封閉的資源'br' –

+0

你是對的......我把它留給他,但我會做一個編輯 – markg

+0

也會更好地使用嘗試資源 – markg

0

看看BufferedReaderScanner閱讀文件。 若要檢查字符串是否包含String-類中的字contains

如果您表現出一些努力,我願意幫助您更多。

0
public static void grep(Reader inReader, String searchFor) throws IOException { 
    BufferedReader reader = null; 
    try { 
     reader = new BufferedReader(inReader); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      if (line.contains(searchFor)) { 
       System.out.println(line); 
      } 
     } 
    } finally { 
     if (reader != null) { 
      reader.close(); 
     } 
    } 
} 

用法:

grep(new FileReader("file.txt"), "GrepMe"); 
相關問題