2

在我的项目中获取资源的本地文件路径的最佳方法是什么?

我有一个包含我要执行的文件 dummy.exe 的 lib 文件夹,但首先我需要知道它在哪里(根据安装目录,每个用户可能会有所不同。

4

4 回答 4

5

安装目录通常是您的 java 应用程序启动的目录,可以从正在运行的 java 应用程序中找到此路径

   System.getProperty("user.dir");

如果要获取相对于应用程序目录的文件的绝对路径,可以使用 File.absolutePath

   String absolutePath = new File("lib/dummy.exe").getAbsolutePath();
于 2013-07-11T11:04:16.380 回答
1

首先,您应该查看Oracle“查找文件”文档

他们列出递归文件匹配并给出示例代码:

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

祝你好运!

于 2013-07-11T10:58:17.580 回答
1

尝试这个

   URL loc = this.getClass().getResource("/file"); 
      String path = loc.getPath(); 
          System.out.println(path);
于 2013-07-11T11:02:29.023 回答
1

所以我说第一个答案对我来说是正确的,但我错了,因为在部署我的应用程序后目录结构根本不匹配。以下代码在 jar 文件中查找资源并返回其本地文件路径。

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

字符串文件路径包含插件内资源的本地文件路径。

于 2013-07-11T12:10:14.253 回答