StackOverflow的第一个问题,所以如果这是不好的道歉。但我为土木工程师找到了一份学生工作计划。我的第一个任务是使用JFileChooser来允许用户指定所需的文件,然后将此文件的完整路径写入txt文件。我希望它自动写入使用JFileChooser的程序所在的文件。我很困惑如何做到这一点,一直没有找到有用的东西。文件I/O混淆
我的代码:
public class FilePathFinder {
JFileChooser fileChooser;
String path;
public static void main(String[] args) throws IOException{
String path = null; //String that will be outputted to
//creates file chooser and its properties
JFileChooser file_chooser = new JFileChooser();
file_chooser.setCurrentDirectory(new java.io.File("user.home"));
file_chooser.setDialogTitle("Create File Path");
file_chooser.setApproveButtonText("Create Path");
file_chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
file_chooser.setAcceptAllFileFilterUsed(false);
if (file_chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
path=(file_chooser.getSelectedFile().getAbsolutePath());
}
//Writes path name to file
String user_home_folder = System.getProperty("user.home");
System.out.println(user_home_folder);
File path_file = new File(user_home_folder, path);
BufferedWriter path_writer = new BufferedWriter(new FileWriter(path_file));
if(!path_file.exists()){
path_writer.write(path);
}
}
}
为了让回答者或其他有类似问题的人更容易,请编辑添加一个特定的问题陈述 - “不起作用”可以假设,但* how *不起作用?什么错误信息或不正确的行为是特征? –