在文本文件中查找字符串的方法。 然后将以下行达到一定限度

所以这就是我到目前为止所拥有的:

public String[] findStudentInfo(String studentNumber) { Student student = new Student(); Scanner scanner = new Scanner("Student.txt"); // Find the line that contains student Id // If not found keep on going through the file // If it finds it stop // Call parseStudentInfoFromLine get the number of courses // Create an array (lines) of size of the number of courses plus one // assign the line that the student Id was found to the first index value of the array //assign each next line to the following index of the array up to the amount of classes - 1 // return string array } 

我知道如何找到一个文件是否包含我试图找到的字符串,但我不知道如何检索它所在的整行。

这是我第一次发帖所以如果我做错了请告诉我。

你可以这样做:

 File file = new File("Student.txt"); try { Scanner scanner = new Scanner(file); //now read the file line by line... int lineNum = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); lineNum++; if() { System.out.println("ho hum, i found it on line " +lineNum); } } } catch(FileNotFoundException e) { //handle this } 

使用Apache Commons IO API https://commons.apache.org/proper/commons-io/我能够使用FileUtils.readFileToString(file).contains(stringToFind)来建立它FileUtils.readFileToString(file).contains(stringToFind)

该function的文档位于https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FileUtils.html#readFileToString(java.io.File)

当您阅读文件时,您是否考虑过逐行阅读? 这将允许您检查您的行是否包含您正在阅读的文件,然后您可以根据它执行您需要的任何逻辑?

 Scanner scanner = new Scanner("Student.txt"); String currentLine; while((currentLine = scanner.readLine()) != null) { if(currentLine.indexOf("Your String")) { //Perform logic } } 

您可以使用变量来保存行号,或者您也可以使用布尔值来指示您是否已通过包含字符串的行:

 Scanner scanner = new Scanner("Student.txt"); String currentLine; int lineNumber = 0; Boolean passedLine = false; while((currentLine = scanner.readLine()) != null) { if(currentLine.indexOf("Your String")) { //Do task passedLine = true; } if(passedLine) { //Do other task after passing the line. } lineNumber++; } 

这是一个在文本文件中查找字符串的java 8方法:

 for (String toFindUrl : urlsToTest) { streamService(toFindUrl); } private void streamService(String item) { String tmp; try (Stream stream = Files.lines(Paths.get(fileName))) { tmp = stream.filter(lines -> lines.contains(item)) .foreach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } 

我在做类似的事情,但在C ++中。 你需要做的是一次读取一行并解析它们(逐个翻译)。 我有一个遍布所有行的内部循环,内部是另一个遍历所有单词的循环。 一旦找到你需要的单词,只需退出循环并返回一个计数器或任何你想要的。

这是我的代码。 它基本上解析了所有单词并将它们添加到“索引”中。 然后将单词所在的行添加到向量中,并用于从索引单词引用该行(包含文件名,整行和行号)。

 ifstream txtFile; txtFile.open(path, ifstream::in); char line[200]; //if path is valid AND is not already in the list then add it if(txtFile.is_open() && (find(textFilePaths.begin(), textFilePaths.end(), path) == textFilePaths.end())) //the path is valid { //Add the path to the list of file paths textFilePaths.push_back(path); int lineNumber = 1; while(!txtFile.eof()) { txtFile.getline(line, 200); Line * ln = new Line(line, path, lineNumber); lineNumber++; myList.push_back(ln); vector words = lineParser(ln); for(unsigned int i = 0; i < words.size(); i++) { index->addWord(words[i], ln); } } result = true; } 

这将在Student.txt中找到“Mark Sagal”。 假设Student.txt包含

Student.txt

 Amir Amiri Mark Sagal Juan Delacruz 

Main.java

 import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { final String file = "Student.txt"; String line = null; ArrayList fileContents = new ArrayList<>(); try { FileReader fReader = new FileReader(file); BufferedReader fileBuff = new BufferedReader(fReader); while ((line = fileBuff.readLine()) != null) { fileContents.add(line); } fileBuff.close(); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(fileContents.contains("Mark Sagal")); } } 

这是TextScanner的代码

 public class TextScanner { private static void readFile(String fileName) { try { File file = new File("/opt/pol/data22/ds_data118/0001/0025090290/2014/12/12/0029057983.ds"); Scanner scanner = new Scanner(file); while (scanner.hasNext()) { System.out.println(scanner.next()); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: java TextScanner1" + "file location"); System.exit(0); } readFile(args[0]); } } 

它将打印带有分隔符的文本