import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class fileReader {
public static void main(String[] args) {
try {
File oFile = new File("myfile.txt");
Scanner oScanner= new Scanner(oFile );
while (oScanner.hasNextLine()) {
String sLine = oScanner.nextLine(); //Next line will point to the next line on the file
System.out.println(sLine ); //And any other operations on the line you would like to perform.
}
oScanner.close();
} catch (FileNotFoundException e) {
System.out.println("Error Occurred");
e.printStackTrace();
}
}
}
1条答案
按热度按时间vybvopom1#
请尝试下面的代码:这里的想法是使用java“scanner”类。这将逐行读取文件,直到到达文件末尾。