如何使用java从包含特定单词的文件中打印行?

时间:2014-02-24 13:57:41

标签: java java-io file-handling

如何使用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());
    }
}

}

4 个答案:

答案 0 :(得分:5)

假设您正在读取名为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();
        }

    }}

希望此代码有所帮助。

答案 1 :(得分:1)

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");

答案 2 :(得分: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();
      }
  }

答案 3 :(得分:0)

请查看BufferedReaderScanner以阅读该文件。 要检查字符串是否包含单词,请使用contains - 类中的String

如果你付出了一些努力,我愿意帮助你。