java 获取资源的本地文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17591523/
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
Get local file path of a ressource
提问by Markus
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.
我有一个包含要执行的文件 dummy.exe 的 lib 文件夹,但首先我需要知道它在哪里(根据安装目录,每个用户的位置可能不同。
采纳答案by Markus
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 文件中查找资源并返回其本地文件路径。
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.
字符串文件路径包含插件内资源的本地文件路径。
回答by Evgeniy Dorofeev
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();
回答by jdero
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!
祝你好运!
回答by Udanesh N
Try this
试试这个
URL loc = this.getClass().getResource("/file");
String path = loc.getPath();
System.out.println(path);