JFileChooser file_chooser = new JFileChooser();
StringBuilder sb = new StringBuilder();
public void pick_me() throws FileNotFoundException {
if(file_chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
File file = file_chooser.getSelectedFile();
Scanner input=new Scanner(file);
String path = file.getAbsolutePath();
while(input.hasNext()) {
sb.append(input.nextLine());
sb.append("\n");
}
input.close();
}
else {
sb.append("No file was selected");
}
}
所以这是我用来让用户浏览文件的方法。我想获取用户选择的文件的文件路径。