选择一个文件,然后在控制台上显示它的内容。
示例代码如下:
//(GUI)文件对话框
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.File;
public class j06{
public static void main(String[] args)throws Exception{
JFileChooser fileChoose1 = new JFileChooser();
if(fileChoose1.showOpenDialog(null)/*显示一个对话框*/ == JFileChooser.APPROVE_OPTION/*APPROVE_OPTION选择打开按钮*/ ){
File file1 = fileChoose1.getSelectedFile();//返回从文件对话框中选中的文件
Scanner input = new Scanner(file1);
while(input.hasNext()){
System.out.println(input.nextLine());
}
input.close();
}
else{
System.out.println("No file selected");
}
}
}