读取InputStream为UTF-8

我试图通过互联网逐行阅读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ó! ,我正在使用它来testing编码。

当我查看OutputStreamout )时,我认为它是> ¬°H√©ll√≥! 。 我不相信这是OutputStream的问题,因为我可以做out.println("é"); 没有问题。

任何读取forms的InputStream为UTF-8的想法? 谢谢!

解决了我自己的问题。 这一行:

 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 

需要是:

 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); 

或者从Java 7开始:

 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)); 
  String file = ""; try { InputStream is = new FileInputStream(filename); String UTF8 = "utf8"; int BUFFER_SIZE = 8192; BufferedReader br = new BufferedReader(new InputStreamReader(is, UTF8), BUFFER_SIZE); String str; while ((str = br.readLine()) != null) { file += str; } } catch (Exception e) { } 

尝试这个,.. :-)