How do I open and view a folder from my computer?
2 answers
See Desktop.open(File)
. For example.
Desktop.getDesktop().open(theDirectory);
SSCCE
import java.awt.Desktop;
import java.io.*;
public class BrowseDirectory {
public static void main(String[] args) throws IOException {
String userHomePath = System.getProperty("user.home");
File userHome = new File(userHomePath);
Desktop.getDesktop().open(userHome);
}
}
Update
Although the directory appears as "My Videos" to the end user, building the file inside the directory and checking the file properties shows that the base name is "Videos".
import java.awt.Desktop;
import java.io.*;
public class BrowseDirectory {
public static void main(String[] args) throws IOException {
String userHomePath = System.getProperty("user.home");
File userHome = new File(userHomePath);
// uses the corect path separator for the OS
File videos = new File(userHome, "Videos");
Desktop.getDesktop().open(videos);
}
}
+5
source to share
try {
String path = "C:\\path\\of\\your\\folder\\";
Runtime runtime = Runtime.getRuntime();
runtime.exec("explorer.exe "+path);
System.out.println("open");
} catch (Exception E) {
}
you can use any path you want but convert it 1st to string and please remember that in java "\" it must be written "\\"
hope it works :)
0
source to share