【问题标题】:Get local file path of a ressource获取资源的本地文件路径
【发布时间】:2013-07-09 14:46:15
【问题描述】:

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

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

【问题讨论】:

  • 如果在 jar 中找到相关资源怎么办?你想怎么处理?
  • 它在一个 jar 文件中,但由于其他原因,我已经将它放在目录结构中而不是 jar 文件中。

标签: java file path resources


【解决方案1】:

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

   System.getProperty("user.dir");

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

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

【讨论】:

  • 由于文件 dummy.exe 总是在我的应用程序中的同一个位置,这种方法最适合我。谢谢你。还要感谢我还将研究的其他答案。
【解决方案2】:

首先,您应该看看Oracle "Finding Files" documentation

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

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

祝你好运!

【讨论】:

    【解决方案3】:

    试试这个

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

    【讨论】:

      【解决方案4】:

      所以我说第一个答案对我来说是正确的,但我错了,因为在部署我的应用程序后目录结构根本不匹配。以下代码在 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);
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-09
        • 1970-01-01
        • 2018-03-08
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        • 2014-07-12
        相关资源
        最近更新 更多