I am now using java Desktop API to manipulate file explorer. I know how to open the file explorer but I don't know how to open it and highlight the specified file.
我现在使用java Desktop API来操作文件资源管理器。我知道如何打开文件资源管理器,但我不知道如何打开它并突出显示指定的文件。
As we using the Chrome, after downloading files, we can choose "show in folder" to open the file explorer and highlight the downloaded file.
当我们使用Chrome时,在下载文件后,我们可以选择“在文件夹中显示”来打开文件资源管理器并突出显示下载的文件。
How to use java Desktop API to do so? Or is there any other API in java can realize this action?
如何使用java Desktop API这样做?或者java中是否有其他API可以实现此操作?
7 个解决方案
#1
28
Use: Runtime.getRuntime().exec("explorer.exe /select," + path);
使用:Runtime.getRuntime()。exec(“explorer.exe / select,”+ path);
This also works if there is a space in the PATH
.
如果PATH中有空格,这也适用。
#2
27
The Desktop API does not support this. You are going to have to use ProcessBuilder
(or alternatively Runtime.exec()
) to execute explorer.exe explicitly with the options you want. This will only work on windows though, if you want to run this on another OS you will have to use the Desktop API anyway.
Desktop API不支持此功能。您将不得不使用ProcessBuilder(或者Runtime.exec())使用您想要的选项显式执行explorer.exe。这只适用于Windows,如果你想在另一个操作系统上运行它,你还是必须使用Desktop API。
Process p = new ProcessBuilder("explorer.exe", "/select,C:\\directory\\selectedFile").start();
#3
25
EDIT:
You cannot highlight a specific file with the java Desktop API.
您无法使用java Desktop API突出显示特定文件。
ANSWER TO ORIGINAL QUESTION:
回答原始问题:
The Desktop API will allow you to do this by using this snippet,
Desktop API允许您使用此代码段执行此操作,
File file = new File ("c:\<directory>");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
The documentation for the code used above is at these links, http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html and http://docs.oracle.com/javase/7/docs/api/java/io/File.html
上面使用的代码文档位于以下链接:http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html和http://docs.oracle.com/javase/ 7 /文档/ API / JAVA / IO / File.html
On a Windows computer this will open the default file explorer and on other systems it will open their default explorers respectively.
在Windows计算机上,这将打开默认文件资源管理器,在其他系统上,它将分别打开其默认资源管理器。
Alternatively you could use the new java Path API to build the required path and then invoke the method that returns the corresponding File object.
或者,您可以使用新的Java Path API来构建所需的路径,然后调用返回相应File对象的方法。
For brevity I excluded the checking code to make sure the Desktop and File objects exist.
为简洁起见,我排除了检查代码以确保Desktop和File对象存在。
#4
3
We can open a specific path from command line with :
我们可以从命令行打开一个特定的路径:
start C:/ProgramData
There are two ways in java you can use to open windows explorer with specific path:
在java中有两种方法可以用来打开具有特定路径的Windows资源管理器:
-
Use Process class(as already answered) but with start command
使用Process类(已经回答)但使用start命令
try { Process builder = Runtime.getRuntime().exec("cmd /c start C:/ProgramData"); } catch (IOException e) { e.printStackTrace(); }
-
Use Desktop class
使用桌面类
try { Desktop.getDesktop().open(new File("C:/ProgramData")); } catch (IOException e) { e.printStackTrace(); }
#5
1
This works even if file/folder name has multiple spaces between words.
即使文件/文件夹名称在单词之间有多个空格,这也有效。
//In this example there are 3 spaces between "GAME" and "OF" and 2 spaces between "OF" and "Thrones"
String onlyPath = "D:\\GAME OF Thrones";
String selectPath = "/select," + onlyPath;
//START: Strip one SPACE among consecutive spaces
LinkedList<String> list = new LinkedList<>();
StringBuilder sb = new StringBuilder();
boolean flag = true;
for (int i = 0; i < selectPath.length(); i++) {
if (i == 0) {
sb.append(selectPath.charAt(i));
continue;
}
if (selectPath.charAt(i) == ' ' && flag) {
list.add(sb.toString());
sb.setLength(0);
flag = false;
continue;
}
if (!flag && selectPath.charAt(i) != ' ') {
flag = true;
}
sb.append(selectPath.charAt(i));
}
list.add(sb.toString());
list.addFirst("explorer.exe");
//END: Strip one SPACE among consecutive spaces
//Output List
for (String s : list) {
System.out.println("string:"+s);
}
/*output of above loop
string:explorer.exe
string:/select,D:\GAME
string: OF
string: Thrones
*/
//Open in Explorer and Highlight
Process p = new ProcessBuilder(list).start();
#6
1
Here is shorter version of above.
这是上面的较短版本。
String onlyPath = "D:\\GAME OF Thrones";
String completeCmd = "explorer.exe /select," + onlyPath;
new ProcessBuilder(("explorer.exe " + completeCmd).split(" ")).start();
#7
0
Always use the "\" instead of "/", otherwise only the Explorer will open, fore more read this - Command-line switches that you can use to open the GUI Windows Explorer (Explorer.exe)
始终使用“\”而不是“/”,否则只有资源管理器才会打开,更多内容请阅读 - 可用于打开GUI的命令行开关Windows资源管理器(Explorer.exe)
Using Windows CLI :
使用Windows CLI:
C:\Users\Md Arif Mustafa>explorer.exe /select, "C:\Users\Md Arif Mustafa\Music\Aafreen-Himesh.mp3"
Same in Java source code : Here variable filePaths is an ArrayList<String>
and contains a folder all files path.
在Java源代码中相同:这里变量filePaths是一个ArrayList
try {
Process proc = Runtime.getRuntime().exec("explorer.exe /select, " + filePaths.get(i).replaceAll("/", "\\\\"));
proc.waitFor();
} catch (IOException | InterruptedException ex ) {
ex.printStackTrace();
}
It worked for me and hope it helps you!
它对我有用,希望它对你有所帮助!
#1
28
Use: Runtime.getRuntime().exec("explorer.exe /select," + path);
使用:Runtime.getRuntime()。exec(“explorer.exe / select,”+ path);
This also works if there is a space in the PATH
.
如果PATH中有空格,这也适用。
#2
27
The Desktop API does not support this. You are going to have to use ProcessBuilder
(or alternatively Runtime.exec()
) to execute explorer.exe explicitly with the options you want. This will only work on windows though, if you want to run this on another OS you will have to use the Desktop API anyway.
Desktop API不支持此功能。您将不得不使用ProcessBuilder(或者Runtime.exec())使用您想要的选项显式执行explorer.exe。这只适用于Windows,如果你想在另一个操作系统上运行它,你还是必须使用Desktop API。
Process p = new ProcessBuilder("explorer.exe", "/select,C:\\directory\\selectedFile").start();
#3
25
EDIT:
You cannot highlight a specific file with the java Desktop API.
您无法使用java Desktop API突出显示特定文件。
ANSWER TO ORIGINAL QUESTION:
回答原始问题:
The Desktop API will allow you to do this by using this snippet,
Desktop API允许您使用此代码段执行此操作,
File file = new File ("c:\<directory>");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
The documentation for the code used above is at these links, http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html and http://docs.oracle.com/javase/7/docs/api/java/io/File.html
上面使用的代码文档位于以下链接:http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html和http://docs.oracle.com/javase/ 7 /文档/ API / JAVA / IO / File.html
On a Windows computer this will open the default file explorer and on other systems it will open their default explorers respectively.
在Windows计算机上,这将打开默认文件资源管理器,在其他系统上,它将分别打开其默认资源管理器。
Alternatively you could use the new java Path API to build the required path and then invoke the method that returns the corresponding File object.
或者,您可以使用新的Java Path API来构建所需的路径,然后调用返回相应File对象的方法。
For brevity I excluded the checking code to make sure the Desktop and File objects exist.
为简洁起见,我排除了检查代码以确保Desktop和File对象存在。
#4
3
We can open a specific path from command line with :
我们可以从命令行打开一个特定的路径:
start C:/ProgramData
There are two ways in java you can use to open windows explorer with specific path:
在java中有两种方法可以用来打开具有特定路径的Windows资源管理器:
-
Use Process class(as already answered) but with start command
使用Process类(已经回答)但使用start命令
try { Process builder = Runtime.getRuntime().exec("cmd /c start C:/ProgramData"); } catch (IOException e) { e.printStackTrace(); }
-
Use Desktop class
使用桌面类
try { Desktop.getDesktop().open(new File("C:/ProgramData")); } catch (IOException e) { e.printStackTrace(); }
#5
1
This works even if file/folder name has multiple spaces between words.
即使文件/文件夹名称在单词之间有多个空格,这也有效。
//In this example there are 3 spaces between "GAME" and "OF" and 2 spaces between "OF" and "Thrones"
String onlyPath = "D:\\GAME OF Thrones";
String selectPath = "/select," + onlyPath;
//START: Strip one SPACE among consecutive spaces
LinkedList<String> list = new LinkedList<>();
StringBuilder sb = new StringBuilder();
boolean flag = true;
for (int i = 0; i < selectPath.length(); i++) {
if (i == 0) {
sb.append(selectPath.charAt(i));
continue;
}
if (selectPath.charAt(i) == ' ' && flag) {
list.add(sb.toString());
sb.setLength(0);
flag = false;
continue;
}
if (!flag && selectPath.charAt(i) != ' ') {
flag = true;
}
sb.append(selectPath.charAt(i));
}
list.add(sb.toString());
list.addFirst("explorer.exe");
//END: Strip one SPACE among consecutive spaces
//Output List
for (String s : list) {
System.out.println("string:"+s);
}
/*output of above loop
string:explorer.exe
string:/select,D:\GAME
string: OF
string: Thrones
*/
//Open in Explorer and Highlight
Process p = new ProcessBuilder(list).start();
#6
1
Here is shorter version of above.
这是上面的较短版本。
String onlyPath = "D:\\GAME OF Thrones";
String completeCmd = "explorer.exe /select," + onlyPath;
new ProcessBuilder(("explorer.exe " + completeCmd).split(" ")).start();
#7
0
Always use the "\" instead of "/", otherwise only the Explorer will open, fore more read this - Command-line switches that you can use to open the GUI Windows Explorer (Explorer.exe)
始终使用“\”而不是“/”,否则只有资源管理器才会打开,更多内容请阅读 - 可用于打开GUI的命令行开关Windows资源管理器(Explorer.exe)
Using Windows CLI :
使用Windows CLI:
C:\Users\Md Arif Mustafa>explorer.exe /select, "C:\Users\Md Arif Mustafa\Music\Aafreen-Himesh.mp3"
Same in Java source code : Here variable filePaths is an ArrayList<String>
and contains a folder all files path.
在Java源代码中相同:这里变量filePaths是一个ArrayList
try {
Process proc = Runtime.getRuntime().exec("explorer.exe /select, " + filePaths.get(i).replaceAll("/", "\\\\"));
proc.waitFor();
} catch (IOException | InterruptedException ex ) {
ex.printStackTrace();
}
It worked for me and hope it helps you!
它对我有用,希望它对你有所帮助!