java - 读取 xml (windows-1252) 文件时出错
问题描述
我在 java 中编写了一个应该读取 xml 文件的程序,但是在使用 windows-1252 读取 xml 文件时出现错误:
java.nio.charset.MalformedInputException:输入长度 = 3
但 UTF-8 对我有用。
public class InputBox {
public static void XmlOeffnen() throws IOException {
JFileChooser chooser = new JFileChooser();
int rueckgabeWert = chooser.showOpenDialog(null);
String content = null;
File f = chooser.getSelectedFile();
String path = f.getAbsolutePath();
try {
content = Files.readString(Paths.get(path), Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
Converter.Konvertieren(chooser.getName(), content, path);
}
}
解决方案
您正在使用默认字符集读取文件。如果要读取具有 Windows-1252 编码的文件,则需要指定该编码。你可以这样做Charset.forName("windows-1252")
更新的行应该是这样的:
content = Files.readString(Paths.get(path), Charset.forName("windows-1252"));
推荐阅读
- python - 在Django中通过URL传递变量
- java - volatile 可以使不同线程的公共变量可见吗?
- python - 拆分字符串保持分隔符没有起始空格
- html - 尝试在打字稿中动态更新 html
- c# - 视图中其他模型的 ASP.NET MVC 显示属性
- javascript - Google Script 函数未返回正确的本地格式日期
- javascript - 进行比较时,对象可能为空错误打字稿
- pandas - 一起运行 df.apply、dask 和 pd.get_dummies
- c - 如何在我的文件 CSV 中写入多行?
- image - 如何将“正确”图像(不是图标)添加到我的 JFrame/JPanel/JComponents (Swing) (Kotlin)