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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14700679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:05:45  来源:igfitidea点击:

How to open and view the folder from my computer?

javafiledirectory

提问by Soorya Thomas

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.

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

回答by Andrew Thompson

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

Desktop.open(File)。例如

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

回答by shadrachJabonir

    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 "\\"

您可以使用任何您想要的路径,但首先将其转换为字符串,请注意在 Java 中“\”应该写为“\\”

hope it works :)

希望它有效:)