Scanner类的应用
1、读取文件内容
给定一个文件的路径,readFile()方法负责读取文件中的内容,nextLine()方法每次读取一行信息
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestScanner {
private static void readFile(String fileName){
try {
Scanner scanner = new Scanner(new File(fileName));
while (scanner.hasNext()) {
String result = scanner.nextLine();
System.out.println(result);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String filename = "F:\\WorkSpace\\Java202105\\springbootdemo\\src\\demo\\TestScanner.java";
readFile(filename);
}
}