[英]How to find a word is in which line of a text file and if the word exists in multiple line save the line number?
我可以找到該單詞的出現,但找不到該單詞存在的行號,也無法像在arraylist中那樣保存行號?
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();
}
}
嘗試使用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();
有一個計數器,為每一行計數。
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");
}
我認為這會有所幫助。
您所要做的就是跟蹤行號,然后將行保存在有單詞的位置
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.