使用文件选择器的选定文件在 Windows 上运行时不会显示为选定文件.在OSX上完美运行



我有一个JavaFX gui程序,它使用文件选择器选择pdf,然后在选定的PDF上运行该程序。我在Mac上对此进行了编程,但是我的一个用户在Windows上。在Mac上一切按预期工作,但是当您单击Windows程序中的选择PDF按钮时,目录会按预期打开,但是当您选择文件并单击确定时,不会返回任何选定内容。

 selectBtn.setOnAction(e -> {
        //Select PDF's Button
        System.out.println("SelectBtn"); //Press to select all ICP-MS PDF files you want to analyze
        //opens file directory to find and select PDF Files
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Select PDF Files");
        fileChooser.getExtensionFilters().addAll(new ExtensionFilter("PDF Files", "*.pdf"));
        List<File> selectedFiles = fileChooser.showOpenMultipleDialog(savedStage);
        for (File selectedFile : selectedFiles) {  // gets all of the path's to selected files and saves them as a string
            String tempFilePath = selectedFile.getAbsolutePath();
            System.out.println(tempFilePath);
            selStrings.add(tempFilePath);
        }
        //shortening up the listview path, need to update list view using this then add a button to increase or decrease the size of path
        for (int i= 0; i < selStrings.size(); i++) {
            String tempFilePath = selStrings.get(i);
            String result[] = tempFilePath.split("/");
            String slash = "/"; 
            String shortFilePath = result[result.length - 3] + slash + result[result.length - 2] + slash + result[result.length - 1];
            shortStrings.add(shortFilePath);
            System.out.println(shortFilePath);
        }
        Collections.sort(selStrings);
        Collections.sort(shortStrings);//sorts alphabetically for initial view in listview
        refreshListView();
    });

知道是什么原因造成的吗?我预计 JVM 可以在两个平台上工作,但没有太多。

我建议您使用:

String slash = FileSystems.getDefault().getSeparator();
String result[] = tempFilePath.split(slash);
String shortFilePath = result[result.length - 3] + slash + result[result.length - 2] + slash + result[result.length - 1];

这样,您将获得独立于操作系统的文件分隔符字符,该字符肯定适用于每个操作系统。我认为Windows不喜欢普通的"/",并且通常使用"\"与MacOS X不同。

我希望我能帮助你:)

最新更新