亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何查找某個單詞在文本文件的哪一行,如果該單詞存在于多行中,則保存行號?

如何查找某個單詞在文本文件的哪一行,如果該單詞存在于多行中,則保存行號?

慕村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 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號