我正在下载html网页的源代码,并将其写回到txt文件。终端上的输出看起来正确,但是在写入文件并使用gedit读取文件内容时,内容看起来像这样:
<^ @!^ @ D ^ @ O ^ @ C ^ @ T ^ @ Y ^ @ P ^ @ E ^ @ ^ @ h ^ @ t ^ @ m ^ @ l ^ @ ^ @ P ^ @ U ^ @ B ^ @ L ^ @ I ^ @ C ^ @ ^ @“ ^ @-^ @ / ^ @ / ^ @ W ^ @ 3 ^ @ C ^ @ / ^ @ / ^ @ D ^ @ T ^ @ D ^ @ ^ @ X ^ @ H ^ @ T ^ @ M ^ @ L ^ @ ^ @ 1 ^ @。^ @ 0 ^ @ ^ @ T ^ @ r ^ @ a ^ @ n ^ @ s ^ @ i ^ @ t ^ @ i ^ @ o ^ @ n ^ @ a ^ @ l ^
我正在使用BufferedReader逐行读取文件,如下所示:
URL oracle = new URL("http://example.com");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
while ((inputLine = in.readLine()) != null)
{
// appending to get the complete html string
}
然后,我使用PrintWriter编写内容。
PrintWriter pout = new PrintWriter("output.txt");
pout.write(html); // here html is the appended html string
pout.close();
有人可以帮我弄这个吗。
在读取URL时,您需要将编码设置为UTF-8,而在回写时,应再次提及您的编码是UTF-8。默认编码可能是系统的编码,可能无法很好地处理unicode字符。无论是InputStream
与Outputstream
支持编码作为参数。因此,您可能希望将其替换PrintWriter
为OutputStream
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句