gpt4 book ai didi

java : Not able to open finder window having space in path

转载 作者:行者123 更新时间:2023-12-02 02:51:28 34 4
gpt4 key购买 nike

我想通过单击按钮打开查找器窗口并突出显示 javafx 中的特定文件,但查找器窗口未打开名称中包含空格的文件夹。相同的代码片段是:-

@FXML
public void openFolder(ActionEvent event) {
try {
String upperCaseOperatingSystem=MCOBillingAppUtils.getOSName();
String path=lblPath.getText();
if(upperCaseOperatingSystem.contains("WINDOW")){
Runtime.getRuntime().exec("explorer.exe /select,"+path);
}else if(upperCaseOperatingSystem.contains("MAC")){
Runtime.getRuntime().exec("open -R "+path);
}
} catch (IOException e) {
e.printStackTrace();
DialogMessageClass.infoBox("Something went Wrong. Please try again sometimes.", "Error");
}catch(Exception e){
e.printStackTrace();
DialogMessageClass.infoBox("Something went Wrong. Please try again sometimes.", "Error");
}
}

路径是:/ME/开发文件夹/Short_Closed/abc.pdf

尝试的路径值是:-/ME/开发文件夹/Short_Closed/abc.pdf

/ME/Development\Folder/Short_Closed/abc.pdf(反斜杠加空格)

最佳答案

对于 Mac(也可能是 Windows),请使用较长的形式 exec :

// note no leading forward slash
String file = "ME/Development Folder/Short_Closed/abc.pdf"
File workingDir = new File("/");
String[] cmd = new String[]{"open", "-R", file};

Runtime.getRuntime().exec(cmd, null, workingDir);

您可以按照您需要的任何方式分解要打开的工作目录和文件。例如

// Note leading forward slash:
File file = new File("/ME/Development Folder/Short_Closed/abc.pdf") ;
File workingDir = file.getParentFile();
String filename = file.getName();

String[] cmd = new String[] {"open", "-R", filename} ;
Runtime.getRuntime().exec(cmd, null, workingDir);

也可以

关于java : Not able to open finder window having space in path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43783824/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com