Java 读写ISO-8859-1编码的文件?
我有ISO-8859-1编码的文件。我试着把它作为一个字符串读入,对它做一些正则表达式替换,然后用相同的编码把它写出来 然而,我得到的结果文件似乎总是UTF-8(至少根据记事本+),会弄乱一些字符 有人知道我做错了什么吗Java 读写ISO-8859-1编码的文件?,java,encoding,io,file-handling,Java,Encoding,Io,File Handling,我有ISO-8859-1编码的文件。我试着把它作为一个字符串读入,对它做一些正则表达式替换,然后用相同的编码把它写出来 然而,我得到的结果文件似乎总是UTF-8(至少根据记事本+),会弄乱一些字符 有人知道我做错了什么吗 private static void editFile(File source, File target) { // Source and target encoding Charset iso88591charset = Charset.forName(&
private static void editFile(File source, File target) {
// Source and target encoding
Charset iso88591charset = Charset.forName("ISO-8859-1");
// Read the file as a single string
String fileContent = null;
try (Scanner scanner = new Scanner(source, iso88591charset)) {
fileContent = scanner.useDelimiter("\\Z").next();
} catch (IOException exception) {
LOGGER.error("Could not read input file as a single String.", exception);
return;
}
// Do some regex substitutions on the fileContent string
String newContent = regex(fileContent);
// Write the file back out in target encoding
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target), iso88591charset))) {
writer.write(newContent);
} catch (Exception exception) {
LOGGER.error("Could not write out edited file!", exception);
}
}
你的代码实际上没有什么问题。Notepad++可以看到以UTF-8编码的文件,因为在基本层面上,UTF-8和您尝试使用的编码之间没有区别。与UTF相比,ISO中只有特定字符不同,并且缺少一些(很多)字符。您可以阅读更多内容,或者只需在谷歌中搜索
ISO-8859-1 vs UTF-8
并使用与ISO编码不同的字符对其进行了测试-结果是IntelliJ(可能还有记事本+)无法轻松检查的文件,我使用的是Linux)识别为ISO-8859-1。除此之外,我还添加了另一个类,它利用了文件类中的新(JDK11)特性。您使用的新扫描仪(source,charset)
是在JDK10中添加的,因此我认为您可能已经使用了11。以下是简化代码:
private static void editFile(File source, File target) {
Charset charset = StandardCharsets.ISO_8859_1;
String fileContent;
try {
fileContent = Files.readString(source.toPath(), charset);
} catch (IOException exception) {
System.err.println("Could not read input file as a single String.");
exception.printStackTrace();
return;
}
String newContent = regex(fileContent);
try {
Files.writeString(target.toPath(), newContent, charset);
} catch (IOException exception) {
System.err.println("Could not write out edited file!");
exception.printStackTrace();
}
}
请随意克隆存储库或在GitHub上检查它,并使用您喜欢的代码版本。您的代码实际上没有任何问题。Notepad++可以看到以UTF-8编码的文件,因为在基本层面上,UTF-8和您尝试使用的编码之间没有区别。与UTF相比,ISO中只有特定字符不同,并且缺少一些(很多)字符。您可以阅读更多内容,或者只需在谷歌中搜索ISO-8859-1 vs UTF-8
并使用与ISO编码不同的字符对其进行了测试-结果是IntelliJ(可能还有记事本+)无法轻松检查的文件,我使用的是Linux)识别为ISO-8859-1。除此之外,我还添加了另一个类,它利用了文件类中的新(JDK11)特性。您使用的新扫描仪(source,charset)
是在JDK10中添加的,因此我认为您可能已经使用了11。以下是简化代码:
private static void editFile(File source, File target) {
Charset charset = StandardCharsets.ISO_8859_1;
String fileContent;
try {
fileContent = Files.readString(source.toPath(), charset);
} catch (IOException exception) {
System.err.println("Could not read input file as a single String.");
exception.printStackTrace();
return;
}
String newContent = regex(fileContent);
try {
Files.writeString(target.toPath(), newContent, charset);
} catch (IOException exception) {
System.err.println("Could not write out edited file!");
exception.printStackTrace();
}
}
请随意克隆存储库或在GitHub上检查它,并使用您喜欢的任何代码版本。我没有看到任何明显的错误,但我有两个建议:直接使用,也许您不需要BufferedWriter
,并且您可以使用相同的写入(字符串)OutputStreamWriter
中的方法。我没有看到任何明显的错误,但我有两个建议:直接使用,也许您不需要BufferedWriter
,您可以使用OutputStreamWriter
中相同的write(String)
方法。