为了账号安全,请及时绑定邮箱和手机立即绑定

如何查找某个单词在文本文件的哪一行,如果该单词存在于多行中,则保存行号?

如何查找某个单词在文本文件的哪一行,如果该单词存在于多行中,则保存行号?

慕村225694 2023-07-19 15:50:03
我可以找到该单词的出现位置,但无法找到该单词所在的行号,以及有什么方法可以像数组列表一样保存行号?  File f1=new File("input.txt")  String[] words=null;  //Intialize the word Array  FileReader fr = new FileReader(f1);  //Creation of File Reader object  BufferedReader br = new BufferedReader(fr);   String s;       String input="Java";   // Input word to be searched  int count=0;   //Intialize the word to zero  while((s=br.readLine())!=null)   //Reading Content from the file  {     words=s.split(" ");  //Split the word using space      for (String word : words)       {             if (word.equals(input))   //Search for the given word             {               count++;    //If Present increase the count by one             }      }  }  if(count!=0)  //Check for count not equal to zero  {     System.out.println("The given word is present for "+count+ " Times in the file");  }  else  {     System.out.println("The given word is not present in the file");  }     fr.close();   }   }
查看完整描述

3 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

有一个计数器并对每一行进行计数。


    long count = 0;

    long lineNumberCounter = 0;

    List<Long> lineNumbers = new ArrayList<>();

    try (BufferedReader b = new BufferedReader(new java.io.FileReader(new File(fileName)))) {


        String readLine = "";


        System.out.println("Reading file using Buffered Reader");


        while ((readLine = b.readLine()) != null) {

            // Here is line number counter

            lineNumberCounter++;

            String[] words = readLine.split(" "); // Split the word using space

            System.out.println(Arrays.toString(words));

            for (String word : words) {

                // Search for the given word

                if (word.trim().equals(input)) {

                    count++; // If Present increase the count by one

                    System.out.println("Word " + input + " found in line " + lineNumberCounter);

                    lineNumbers.add(lineNumberCounter);

                }

            }

        }

    }


    // Check for count not equal to zero

    if (count != 0) {

        System.out.println("The given word is present for " + count + " Times in the file");

    } else {

        System.out.println("The given word is not present in the file");

    }


查看完整回答
反对 回复 2023-07-19
?
蓝山帝景

TA贡献1843条经验 获得超7个赞

我认为这会有所帮助。

您所要做的就是跟踪行号,然后保存该单词可用的行


File f1=new File("input.txt")

String[] words=null;  //Intialize the word Array

FileReader fr = new FileReader(f1);  //Creation of File Reader object

BufferedReader br = new BufferedReader(fr); 

String s;     

String input="Java";   // Input word to be searched

int count=0;   //Intialize the word to zero


// for keeping track of the line numbers

int lineNumber= 0; 


//arraylist to save the numbers

List<int> lineNumberList = new ArrayList<>();


while((s=br.readLine())!=null)   //Reading Content from the file

{

  // increase the line number as we move on to the next line

  lineNumber++;


 words=s.split(" ");  //Split the word using space


  // this is required so that same line number won't be repeated on the arraylist

  boolean flag = true;

  for (String word : words) 

  {


         if (word.equals(input))   //Search for the given word

         {

           count++;    //If Present increase the count by one

           if(flag){

               lineNumberList.add(lineNumber);

               flag=false;

            }

         }

  }

 }

if(count!=0)  //Check for count not equal to zero

 {

 System.out.println("The given word is present for "+count+ " Times in the file");

 }

 else

  {

 System.out.println("The given word is not present in the file");

 }


 fr.close();

 }

}


查看完整回答
反对 回复 2023-07-19
?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

尝试使用LineNumberReader而不是BufferedReader. 它支持 BufferedReader 和 LineNumber。


Javadoc 了解更多信息 - https://docs.oracle.com/javase/8/docs/api/java/io/LineNumberReader.html。


例子 -


LineNumberReader lineNumberReader = 

    new LineNumberReader(new FileReader("c:\\data\\input.txt"));


int data = lineNumberReader.read();

while(data != -1){

    char dataChar = (char) data;

    data = lineNumberReader.read();

    // your word processing happens here

    int lineNumber = lineNumberReader.getLineNumber();

}

lineNumberReader.close();

http://tutorials.jenkov.com/java-io/linenumberreader.html


查看完整回答
反对 回复 2023-07-19
  • 3 回答
  • 0 关注
  • 127 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信