如何从我的电脑打开和查看文件夹?

时间:2013-02-05 05:19:57

标签: java file directory

编辑问题:

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


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

    }

我在java.awt.Desktop类中打开了错误方法,无法应用于给定的类型。

2 个答案:

答案 0 :(得分:5)

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);
    }
}

更新

虽然目录对最终用户显示为“我的视频”,但在目录中形成文件并检查文件的属性,显示基础名称为“视频”。

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);
    }
}

答案 1 :(得分:0)

    try {
            String path = "C:\\path\\of\\your\\folder\\";
            Runtime runtime = Runtime.getRuntime();
            runtime.exec("explorer.exe "+path);
            System.out.println("open");
        } catch (Exception E) {
        }

你可以使用你想要的任何路径,但首先将它转换为字符串,请注意java“\”应该写成“\\”

希望它有效:)