java文件名特殊字符_Java 8:用名字读取特殊字符的文件
我試圖在Linux系統(tǒng)上讀取名稱中包含特殊字符的文件。我對(duì)操作系統(tǒng)沒有任何控制權(quán)。
我試過使用IO和NIO。我不斷地
java.nio.file.invalidPathException:格式錯(cuò)誤的輸入或輸入包含
不可映射字符:/mnt/au?Enr?ckspiegel,elektrisch verstellbar,1,edition-meta.xml
我不能在服務(wù)器上改變太多,我能在我的應(yīng)用程序中做些什么來解決這個(gè)問題嗎?
System.out.println("Default Charset=" + Charset.defaultCharset()); // US_ASCII
這給了我一個(gè)特殊字符有問號(hào)的字符串/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();
}
});
總結(jié)
以上是生活随笔為你收集整理的java文件名特殊字符_Java 8:用名字读取特殊字符的文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python编写程序模拟硬币的投掷、假设
- 下一篇: MySQL DQL语言的笔记