【发布时间】:2017-07-08 17:30:08
【问题描述】:
我有这个包含大量 unicode 的文本文件,并试图在控制台中打印相应的 UTF-8 字符,但它打印的只是十六进制字符串。就像我复制任何值并将它们粘贴到 System.out 一样,它可以正常工作,但从文本文件中读取它们时就不行了。
以下是我读取文件的代码,其中包含诸如 \u00C0、\u00C1、\u00C2、\u00C3 之类的值行,这些值会打印到控制台,而不是我想要的值。
private void printFileContents() throws IOException {
Path encoding = Paths.get("unicode.txt");
try (Stream<String> stream = Files.lines(encoding)) {
stream.forEach(v -> { System.out.println(v); });
} catch (IOException e) {
e.printStackTrace();
}
}
这是我用来解析首先包含 unicode 的 html 的方法。
private void parseGermanEncoding() {
try
{
File encoding = new File("encoding.html");
Document document = Jsoup.parse(encoding, "UTF-8", "http://example.com/");
Element table = document.getElementsByClass("codetable").first();
Path f = Paths.get("unicode.txt");
try (BufferedWriter wr = new BufferedWriter(new FileWriter(f.toFile())))
{
for (Element row : table.select("tr"))
{
Elements tds = row.select("td");
String unicode = tds.get(0).text();
if (unicode.startsWith("U+"))
{
unicode = unicode.substring(2);
}
wr.write("\\u" + unicode);
wr.newLine();
}
wr.flush();
wr.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
【问题讨论】:
-
你是不是在你的文件里写了
\u00C2等等?请向我们展示文本文件的一部分 -
文本文件如下所示。 '\u00C0 \u00C1 \u00C2 \u00C3 \u00C4 \u00C5 \u00C6 \u00C7 \u00C8 \u00C9 \u00CA \u00CB \u00CC \u00CD \u00CE \u00CF \u00D0 \u00D1 \u00D2 \u00D3 \u00D4'
-
抱歉,打印不正确。基本上,这些值中的每一个都在单独的行上。
-
在原帖中添加了更多内容。
标签: java file parsing utf-8 path