【问题标题】:How to print lines from a file that contain a specific word using java?如何使用java从包含特定单词的文件中打印行?
【发布时间】:2014-03-26 05:47:12
【问题描述】:

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

}

【问题讨论】:

  • 您可以使用代表每一行的字符串实例中的contains 方法。如果您想避免误报匹配,您可以使用带有单词边界的正则表达式。无论如何,请描述您的代码有什么问题,以便我们帮助您解决它。
  • 到目前为止你尝试过什么?请给一些代码...
  • 你能逐行读取文件吗?如果是,为什么没有任何代码存在?
  • 我是java初学者..也是这个网站..所以不知道如何发布问题...但我愿意努力工作,所以想知道各种解决上述问题的方法...

标签: java java-io file-handling


【解决方案1】:

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

    }}

希望这段代码有所帮助。

【讨论】:

    【解决方案2】:
    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");
    

    【讨论】:

      【解决方案3】:

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

      如果你表现出一些努力,我愿意为你提供更多帮助。

      【讨论】:

        【解决方案4】:

        你需要做这样的事情

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

        【讨论】:

        • 1.不要吞下异常 2. 你的代码可以留下未关闭的资源br
        • 你是对的......我把它留给他,但我会进行编辑
        • 用资源试试会更好
        猜你喜欢
        • 2015-07-20
        • 1970-01-01
        • 1970-01-01
        • 2015-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-07
        相关资源
        最近更新 更多