可视化弹出选择框,选择文件
返回未string
public String chooseSavedFile() {
int result = 0;
File file = null;
String path = null;
JFileChooser fileChooser = new JFileChooser();
FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句
System.out.println(fsv.getHomeDirectory()); //得到桌面路径
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
fileChooser.setDialogTitle("请选择要保存的文件...");
fileChooser.setApproveButtonText("确定");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
JFrame chatFrame = new JFrame();
result = fileChooser.showOpenDialog(chatFrame);
if (JFileChooser.APPROVE_OPTION == result) {
path=fileChooser.getSelectedFile().getPath();
if(path.endsWith(".json")) {
System.out.println("path: "+path);
return path;
}
else {
JOptionPane.showMessageDialog(null, "请选择json文件","ERROR",JOptionPane.ERROR_MESSAGE);
}
}
return "";
}