我有一个Java项目,该项目会打开一个对话框,您可以通过以下方法选择要获取其路径的文件:
private void importFile() {
try {
FileDialog dialog = new FileDialog((Frame) null, "Select file");
dialog.setMode(FileDialog.LOAD);
dialog.setVisible(true);
filenameLabel.setText(dialog.getFile());
filePath = new File(dialog.getFile()).getCanonicalPath();
System.out.println(filePath);
String outputString = output.getText();
output.setText(outputString + newline + "Succesfully imported!");
} catch (Exception e) {
e.printStackTrace();
filenameLabel.setText("no file imported");
String outputString = output.getText();
output.setText(outputString + newline + "File not imported!");
}
}
输出看起来像这样desktop/folder/project-directory/name-of-selected-file.csv
,而不是实际路径,实际路径类似于desktop/folder/folder/input/name-of-selected-file.csv
。我该怎么办? filePath
变量被发送到其他类,没有它,应用程序将无法工作