0
我正在使用SimpleFileVisitor
来搜索文件。它在Windows和Linux上运行良好。但是,当我尝试在Unix上使用它时,如操作系统它不能按预期工作。我会得到这样的错误:如何在Java中使用SimpleFileVisitor来查找编码可能不同的文件名?
java.nio.file.NoSuchFileException:
/File/Location/MyFolder/\u0082\u0096\u0096âĜu0099\u0081\u0097K
\u0097\u0099\u0096\u0097\u0085\u0099Ĝu0089\u0085
它看起来像获取的名称是在不同的字符编码,也许这是什么原因造成的问题。它看起来像在获取名称和尝试获取文件访问之间,编码正在被遗漏。对于每个试图访问的文件,这个结果都会调用preVisitDirectory
然后visitFileFailed
。我不知道为什么walkFileTree
方法是这样做的。任何想法?我使用SimpleFileVisitor
代码
看起来是这样的:
Files.walkFileTree(serverLocation, finder);
我SimpleFileVisitor
类:
public class Finder extends SimpleFileVisitor<Path> {
private final PathMatcher matcher;
private final List<Path> matchedPaths = new ArrayList<Path>();
private String usedPattern = null;
Finder(String pattern) {
this.usedPattern = pattern;
matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
}
void match(Path file) { //Compare pattern against file or dir
Path name = file.getFileName();
if (name != null && matcher.matches(name))
matchedPaths.add(file);
}
// Check each file.
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
match(file);
return CONTINUE;
}
// Check each directory.
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
match(dir);
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException e) {
System.out.println("Issue: " + e);
return CONTINUE;
}
通常,当您看到“?”字符时,表示您有一个正在被解释为ISO-8859-1或WIN-1252的UTF-8流。 “â”是来自UTF-8的多字节指示,它是错误翻译的。 –