How do I open and view a folder from my computer?

Edited question:

try{
    folder=jTextField1.getText()+"_portfolio";


        String path="E:/test folder/"+folder+"";
    Desktop.getDesktop().open(path);
    }catch(Exception E){

    }

      

I got the error method on the java.awt.Desktop class cannot be applied to the given types.

+3


source to share


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


    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







All Articles