Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open and view the 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 error method open in class java.awt.Desktop cannot be applied to given types.

like image 426
Soorya Thomas Avatar asked Oct 17 '25 17:10

Soorya Thomas


2 Answers

See Desktop.open(File). E.G.

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, forming a file inside the directory and checking the properties of the file, reveals the underlying 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);
    }
}
like image 191
Andrew Thompson Avatar answered Oct 19 '25 08:10

Andrew Thompson


    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 that you want but convert it 1st to string and please be aware in java "\" should written "\\"

hope it works :)

like image 44
shadrachJabonir Avatar answered Oct 19 '25 08:10

shadrachJabonir