Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use java code to open Windows file explorer and highlight the specified file?

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.

As we using the Chrome, after downloading files, we can choose "show in folder" to open the file explorer and highlight the downloaded file.

How to use java Desktop API to do so? Or is there any other API in java can realize this action?

like image 489
Charles Wu Avatar asked Sep 09 '11 06:09

Charles Wu


People also ask

How do I open Java File Explorer?

Opening file explorer with java in any platform – Windows, Linux and Mac. The easiest way to do this is by using Java Desktop. The class provides an open() function Desktop#open(File) to open explorer when the given file is a directory. From Java 9 onwards, Desktop is available on the java.

How do I find a specific File in Java?

Searching files in Java can be performed using the File class and FilenameFilter interface. The FilenameFilter interface is used to filter files from the list of files. This interface has a method boolean accept(File dir, String name) that is implemented to find the desired files from the list returned by the java. io.

How do you open a File and write to a File in Java?

Use a FileWriter object, and use the constructor that takes the File object and a boolean, the latter if true would allow appending text into the File if it exists. Then initialize a PrintWriter passing in the FileWriter into its constructor.


1 Answers

Use: Runtime.getRuntime().exec("explorer.exe /select," + path);

This also works if there is a space in the PATH.

like image 82
Stone Avatar answered Sep 30 '22 17:09

Stone