java文件名特殊字符_Java 8:用名字读取特殊字符的文件
我試圖在Linux系統上讀取名稱中包含特殊字符的文件。我對操作系統沒有任何控制權。
我試過使用IO和NIO。我不斷地
java.nio.file.invalidPathException:格式錯誤的輸入或輸入包含
不可映射字符:/mnt/au?Enr?ckspiegel,elektrisch verstellbar,1,edition-meta.xml
我不能在服務器上改變太多,我能在我的應用程序中做些什么來解決這個問題嗎?
System.out.println("Default Charset=" + Charset.defaultCharset()); // US_ASCII
這給了我一個特殊字符有問號的字符串/mnt/au?sen.xml“
Files.list(Paths.get(path)).forEach(file -> {
log.info("file to string: " + file.toString());
String correctedFileName = "";
correctedFileName = new String(file.getFileName().toString().getBytes(StandardCharsets.ISO_8859_1),
StandardCharsets.UTF_8);
log.info("corrected name: " + correctedFileName);
try {
ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(Paths.get(correctedFileName)));
ZipEntry entry = zipInputStream.getNextEntry();
while (entry != null) {
Path filePath = Paths.get(unzipLocation, entry.getName());
if (!entry.isDirectory()) {
unzipFiles(zipInputStream, filePath);
} else {
Files.createDirectories(filePath);
}
zipInputStream.closeEntry();
entry = zipInputStream.getNextEntry();
}
} catch (IOException e) {
e.printStackTrace();
}
});
總結
以上是生活随笔為你收集整理的java文件名特殊字符_Java 8:用名字读取特殊字符的文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python编写程序模拟硬币的投掷、假设
- 下一篇: MySQL DQL语言的笔记