1. Exam Points
Read data from a file. (File, Scanner, nextXXX() methods)- Read data using
loops. ( hasNext() method + nextXXX() method)- Use the
splitmethod of the String class to split the content read.Note: use nextXXX() method only once in the loop body.
2. Knowledge Points
(1) Read data from file
- A
fileis storage for data thatpersists(持久化)when the program is not running. - In a program, we can
retrieve data from a file. - To read data from a file, you can use the
FileandScannerclasses - Example 1: read data from a file
// the classes used must be imported using import statements. import java.io.File; import java.io.IOException; import java.util.Scanner; public class Demo{ // add the 'throws IOException' statement in the header of the method public static void main(String[] args) throws IOException { // create a File object and specify the name of the file to read File file = new File("file1.txt"); // create a Scanner object, and specify the File object to use Scanner sc = new Scanner(file); // read an integer value from the file int data1 = sc.nextInt(); // read a double value from the file double data2 = sc.nextDouble(); // read a boolean value from the file boolean data3 = sc.nextBoolean(); // read a String value from the file String data4=sc.next(); // read the rest of the current line from the file String data5=sc.nextLine(); // close the Scanner object once reading is done sc.close(); } } - Example 2: read data from a file
using loops
File file = new File("file2.txt"); Scanner sc = new Scanner(file); while (sc.hasNext()) { // check if there is next item to read String str = sc.next(); // get the next String String[] strs = str.split("_"); // split the String using '_' System.out.println(strs[0] + " " + strs[1]); // print each part of the String } Note
- the
typesof data retrieved mustmatchthe types of data in the file. - Example, if you read integer, boolean, String values one by one, then the values must be in the order of integer, boolean, String in the file.
- The
FileandIOExceptionclasses are from the java.io package. Import statementsmust be used to make these classes available for use in the program.
- the
(2) Methods of the Scanner class
int nextInt(): returns the next int read from the file.double nextDouble(): returns the next double read from the file.boolean nextBoolean(): returns the next boolean value read from the file.String next(): returns the next String value read from the file.String nextLine(): returns the rest content in the current line.boolean hasNext(): returns true if there is a next item to read in the file, or false otherwise.void close(): close the scanner.
(3) Methods of the String class
String[] split(String x): returns a String array where each element is a substring of this String, which has been split around matches of the given expression x.- Example:
String text = "apple,orange,pear,banana,avocado"; // the String text is split into five Strings using ',' as a separator String[] fruits = text.split(","); // after splitting, the fruits array has five elements.