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

【字号: 日期:2024-04-11浏览:38作者:雯心
如何解决在文本文件中查找字符串的方法。然后使以下行达到一定限制?

您可以执行以下操作:

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(<some condition is met for the line>) { System.out.println('ho hum, i found it on line ' +lineNum);} }} catch(FileNotFoundException e) { //handle this}解决方法

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

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}

我知道如何查找文件是否包含我要查找的字符串,但是我不知道如何检索其所在的整行。

这是我的第一次发布,所以如果我做错了什么,请告诉我。

相关文章: