我正在尝试通过 Internet 逐行读取 text/plain
文件。我现在拥有的代码是:
URL url = new URL("http://kuehldesign.net/test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
LinkedList<String> lines = new LinkedList();
String readLine;
while ((readLine = in.readLine()) != null) {
lines.add(readLine);
}
for (String line : lines) {
out.println("> " + line);
}
文件 test.txt
包含 ¡Hélló!
,我使用它来测试编码。
当我查看 OutputStream
( out
) 时,我将其视为 > ¬°H√©ll√≥!
。我不认为这是 OutputStream
的问题,因为我可以毫无问题地做到 out.println("é");
。
将 InputStream
阅读成 UTF-8 有什么想法吗?谢谢!
原文由 Chris Kuehl 发布,翻译遵循 CC BY-SA 4.0 许可协议
解决了我自己的问题。这一行:
需要是:
或者从 Java 7 开始: