英文:
Check if File / Path is Alias, Shortcut or Symbolic link
问题
我需要遍历文件夹中的文件,并找到所有的图像。我需要包括子目录,以及制作的快捷方式(在Windows上),别名(对于Mac用户)或符号链接。
我找到了一个只能用于符号链接的工作API...我似乎无法确定一个文件是别名还是快捷方式..
我有的代码:
private List<Path> getFiles(String folder) throws IOException {
List<Path> files = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(folder))) {
for (Path p : stream) {
System.out.println(p);
if(isLink(p.toFile())) {
files.addAll(getFiles(p.toFile().getCanonicalPath()));
}
String mimeType = Files.probeContentType(p);
if (StringUtils.isNotBlank(mimeType) && mimeType.contains("image")) {
files.add(p);
}
}
return files;
}
}
public static boolean isLink(File file) {
try {
if (!file.exists())
return true;
else {
String cnnpath = file.getCanonicalPath();
String abspath = file.getAbsolutePath();
return !abspath.equals(cnnpath);
}
} catch (IOException ex) {
System.err.println(ex);
return true;
}
}
一些帮助将不胜感激!
谢谢!
英文:
I need to traverse files in a folder, and find all images. I need to include subdirectories, as well as made shortcuts (on windows), aliases (for mac users) or symbolic links.
I found working API that only works for the symbolic links.. I can't seem to determine if a file is an Alias or a shortcut..
Code I have:
private List<Path> getFiles(String folder) throws IOException {
List<Path> files = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(folder))) {
for (Path p : stream) {
System.out.println(p);
if(isLink(p.toFile())) {
files.addAll(getFiles(p.toFile().getCanonicalPath()));
}
String mimeType = Files.probeContentType(p);
if (StringUtils.isNotBlank(mimeType) && mimeType.contains("image")) {
files.add(p);
}
}
return files;
}
and
public static boolean isLink(File file) {
try {
if (!file.exists())
return true;
else {
String cnnpath = file.getCanonicalPath();
String abspath = file.getAbsolutePath();
return !abspath.equals(cnnpath);
}
} catch (IOException ex) {
System.err.println(ex);
return true;
}
}
Some help would be greatly appreciated!
Thanks!
答案1
得分: 1
To avoid toFile()
calls you could also use Files.isSymbolicLink(Path p)
and p.toRealPath()
to determine the target of symbolic links for the paths you use.
Windows shortcuts are not modelled in Java File / Path APIs. An implementation which reads Windows shortcut binary format can be found here in another SO question - it's better if using drive letters, not using UNC pathnames.
I can't help for MacOS alias.
You should keep track of the directories visited in getFiles to avoid an infinite loop when following links.
英文:
To avoid toFile()
calls you could also use Files.isSymbolicLink(Path p)
and p.toRealPath()
to determine the target of symbolic links for the paths you use.
Windows shortcuts are not modelled in Java File / Path APIs. An implementation which reads Windows shortcut binary format can be found here in another SO question - it better if using drive letters not using UNC pathnames.
I can't help for MacOS alias.
You should keep track of the directories visited in getFiles to avoid infinite loop when following links.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论