用java语言怎样写一段程序读d盘的text.txt文件?

2024-12-20 21:19:24
推荐回答(1个)
回答1:

/*Node.java*/
import java.io.File;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;

public class Node{
public static void main(String args[]){
File file = new File("D:\\text.txt");
try{
Scanner scanner = new Scanner(new FileInputStream(file),"gbk");
while(scanner.hasNextLine(){
System.out.println(scanner.nextLine());
}
scanner.close();
}catch(IOException e){
scanner.close();
System.out.println("exception");
}
}
}

javac编译
java运行即可