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

如何使用java打印包含特定单词的文件中的行?,java,java-io,file-handling,Java,Java Io,File Handling,如何使用java打印包含特定单词的文件中的行 希望创建一个简单的实用程序,允许在文件中查找单词并打印给定单词所在的完整行 我已经做了这么多的事情来计算发生率,但不知道如何打印包含它的行 import java.io.*; public class SearchThe { public static void main(String args[]) { try {

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

}

您需要这样做

   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();
      }
  }

查看
BufferedReader
Scanner
以读取文件。 要检查字符串是否包含单词,请使用
字符串
-类中的
包含

如果你表现出一些努力,我愿意更多地帮助你

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

假设您正在读取名为file1.txt的文件,则可以使用以下代码打印包含特定单词的所有行。假设你正在搜索“foo”这个词


希望这段代码有帮助。

您可以使用表示每行的字符串实例中的
contains
方法。如果要避免误报匹配,可以使用带有单词边界的正则表达式。无论如何,请描述一下你的代码有什么问题,这样我们就可以帮助你解决它。到目前为止你都尝试了什么?请给我一些代码……你能逐行阅读文件吗?如果是的话,为什么没有代码呢?我是java的初学者。也是这个网站的初学者。所以我不知道如何发帖…但我愿意努力工作,所以想知道解决上述问题的各种方法…1。不要吞下例外2。您的代码可以保留未关闭的资源
br
您是对的。。。我让他来决定,但我会做一个编辑,如果使用try和资源,也会更好
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();
        }

    }}