[英]Handling unicode escaping in Java InputStream
I am reading json files to String, I sometimes update them (replace some specific words by others) and then write those update json files in a zip. This is what an example input file looks like:我正在将 json 文件读取为字符串,我有时会更新它们(用其他单词替换一些特定的单词),然后将这些更新 json 文件写入 zip。这是一个示例输入文件的样子:
The issue is, the output "loses" character escaping and is therefore no longer a valid json:问题是,output“丢失”字符 escaping,因此不再是有效的 json:
To read the json:读取 json:
private InputStream processJsonFile(File file) throws IOException {
String content;
if (!file.exists())
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
try {
content = IOUtils.toString(new FileReader(file, StandardCharsets.UTF_8));
} catch (IOException | NullPointerException e) {
Logs.logError("Error while reading file " + file.getPath());
Logs.logError("It seems to be malformed!");
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
} finally {
IOUtils.closeQuietly();
}
// here i do things with content
return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
}
to add an inputstream to the zip file:将输入流添加到 zip 文件:
try (fis) {
while ((length = fis.read(bytes)) >= 0)
zos.write(bytes, 0, length);
}
You're outputting raw Unicode values, when you should be encoding them as Unicode constants eg \unnnn
.您正在输出原始 Unicode 值,而您应该将它们编码为 Unicode 常量,例如
\unnnn
。
Use a library to encode the content, eg使用库对内容进行编码,例如
content = EntityUtils.toString(content,"UTF-8");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.