In the previous chapter we learned how to use objects of the Scanner class to parse data in strings, so in this section we will learn how to use objects of the Scanner class to parse data in files.
The application may need to parse special data in the file. At this time, the application can read all the contents of the file into the memory and then parse the required content. The advantage is that the processing speed is fast, but if the read content is large, it will consume More memory means trading space for time.
This section mainly introduces how to use the Scanner class and regular expressions to parse files, for example, to parse out special words, numbers and other information in the file. The characteristic of using the Scanner class and regular expressions to parse files is that time is exchanged for space, that is, the parsing speed is relatively slow, but memory is saved.
Create a Scanner object and point to the file to be parsed, for example:
Filefile=newFile(hello.java);Scannersc=newScanner(file);
Then sc uses spaces as delimiters and calls the next() method to return the words in file in sequence. If the last word of file has been returned by the next() method, sc calls hasNext() to return false, otherwise it returns true.
In addition, for numeric words, such as 108, 167.92, etc., you can use the nextInt() or nextDouble() method instead of the next() method, that is, sc can call the nextInt() or nextDouble() method to convert the numeric words into int or Double data is returned, but it is important to note that if the word is not a numeric word, an InputMismatchException will occur when calling the nextInt() or nextDouble() method. When handling the exception, the next() method can be called to return the non-numeric word.
Create a Scanner object, point to the file to be parsed, and use the useDelimiter method to specify the regular expression as the delimiter mark, for example:
Filefile=newFile(hello.java);Scannersc=newScanner(file);sc.useDelimiter(regular expression);
Then sc uses the regular expression as a delimiter and calls the next() method to return the words in the file in sequence. If the last word of the file has been returned by the next() method, sc calls hasNext() and it will return false, otherwise it will return true.
In addition, for numeric words, such as 1979, 0.618, etc., you can use the nextInt() or nextDouble() method instead of the next() method, that is, sc can call the nextInt() or nextDouble() method to convert the numeric words into int or Double data is returned, but it is important to note that if the word is not a numeric word, an InputMismatchException will occur when calling the nextInt() or nextDouble() method. Then when handling the exception, you can call the next() method to return the non-numeric word.
For example, use the regular expression (matches all non-numeric strings) String regex=[^0123456789.]+ as the delimited mark to parse the student grades in the student.txt file and calculate the average grade.
student.txt
Zhang San's score is 70 points, Li Si's score is 80 points, and Zhao Wu's score is 90 points.
The code is as follows:
importjava.io.*;importjava.util.*;publicclassMain{publicstaticvoidmain(Stringargs[]){Filefile=newFile(student.txt);Scannersc=null;intcount=0;doublesum=0;try{doublescore=0;sc= newScanner(file);sc.useDelimiter([^0123456789.]+);while(sc.hasNextDouble()){score=sc.nextDouble();count++;sum=sum+score;System.out.println(score) ;}doubleaver=sum/count;System.out.println(average score:+aver);}catch(Exceptionexp){System.out.println(exp);}}}