简体   繁体   English

获取资源的本地文件路径

[英]Get local file path of a ressource

what is the best way to get the local file path of a ressource inside of my project? 在项目中获取资源的本地文件路径的最佳方法是什么?

I have a lib folder containing the file dummy.exe which I want to execute but first I need to know where it is (Which can be different for each user, based on the install directory. 我有一个lib文件夹,其中包含我想要执行的文件dummy.exe但首先我需要知道它在哪里(根据安装目录,每个用户可能有所不同。

The install directory is typically the directory where your java app is started, this path can be found from a running java app as 安装目录通常是启动Java应用程序的目录,此路径可以从正在运行的Java应用程序中找到

   System.getProperty("user.dir");

If you want to get absolute path of a file relative to app dir you can use File.absolutePath 如果要获取相对于应用程序目录的文件的绝对路径,则可以使用File.absolutePath

   String absolutePath = new File("lib/dummy.exe").getAbsolutePath();

First, you should take a look at the Oracle "Finding Files" documentation . 首先,您应该看一下Oracle“查找文件”文档

They list recursive file matching and give example code: 他们列出了递归文件匹配并给出示例代码:

public class Find {

    public static class Finder
        extends SimpleFileVisitor<Path> {

        private final PathMatcher matcher;
        private int numMatches = 0;

        Finder(String pattern) {
            matcher = FileSystems.getDefault()
                    .getPathMatcher("glob:" + pattern);
        }

        // Compares the glob pattern against
        // the file or directory name.
        void find(Path file) {
            Path name = file.getFileName();
            if (name != null && matcher.matches(name)) {
                numMatches++;
                System.out.println(file);
            }
        }

        // Prints the total number of
        // matches to standard out.
        void done() {
            System.out.println("Matched: "
                + numMatches);
        }

        // Invoke the pattern matching
        // method on each file.
        @Override
        public FileVisitResult visitFile(Path file,
                BasicFileAttributes attrs) {
            find(file);
            return CONTINUE;
        }

        // Invoke the pattern matching
        // method on each directory.
        @Override
        public FileVisitResult preVisitDirectory(Path dir,
                BasicFileAttributes attrs) {
            find(dir);
            return CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file,
                IOException exc) {
            System.err.println(exc);
            return CONTINUE;
        }
    }

    static void usage() {
        System.err.println("java Find <path>" +
            " -name \"<glob_pattern>\"");
        System.exit(-1);
    }

    public static void main(String[] args)
        throws IOException {

        if (args.length < 3 || !args[1].equals("-name"))
            usage();

        Path startingDir = Paths.get(args[0]);
        String pattern = args[2];

        Finder finder = new Finder(pattern);
        Files.walkFileTree(startingDir, finder);
        finder.done();
    }
}

Best of luck! 祝你好运!

So I said that the first answer was correct for me but I was wrong since the directory structure doesn't match at all after deploying my application. 所以我说第一个答案对我来说是正确的,但是我错了,因为在部署我的应用程序后目录结构根本不匹配。 The following code finds a ressource inside a jar file and returns its local filepath. 以下代码在jar文件中查找ressource并返回其本地文件路径。

    String filepath = "";

    URL url = Platform.getBundle(MyPLugin.PLUGIN_ID).getEntry("lib/dummy.exe");

    try {
        filepath = FileLocator.toFileURL(url).toString();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    System.out.println(filepath);

The string filepath contains the local filepath of the ressource which is inside a plugin. 字符串文件路径包含插件内的ressource的本地文件路径。

Try this 尝试这个

   URL loc = this.getClass().getResource("/file"); 
      String path = loc.getPath(); 
          System.out.println(path);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM