gpt4 book ai didi

java - 用Java从互联网读取UTF-8编码的文本文件

转载 作者:行者123 更新时间:2023-12-02 13:01:23 25 4
gpt4 key购买 nike

我想从互联网上读取 xml 文件。可以找到here .
问题是它是用 UTF-8 编码的,我需要将它存储到一个文件中以便稍后解析它。我已经阅读了很多相关主题,以下是我的想法:

BufferedReader in;
String readLine;
try
{
in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
BufferedWriter out = new BufferedWriter(new FileWriter(file));

while ((readLine = in.readLine()) != null)
out.write(readLine+"\n");

out.close();
}

catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}

catch (IOException e)
{
e.printStackTrace();
}

此代码一直有效,直到这一行:<title>Chérie FM</title>
当我调试时,我得到这个:<title>Ch�rie FM</title>

显然,有些东西我无法理解,但在我看来,我遵循了在几个网站上看到的代码。

最佳答案

此文件未编码为 UTF-8,而是 ISO-8859-1

将代码更改为:

BufferedReader in;
String readLine;
try
{
in = new BufferedReader(new InputStreamReader(url.openStream(), "ISO-8859-1"));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file) , "UTF-8"));

while ((readLine = in.readLine()) != null)
out.write(readLine+"\n");
out.flush();
out.close();
}

catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}

catch (IOException e)
{
e.printStackTrace();
}

您应该得到预期的结果。

关于java - 用Java从互联网读取UTF-8编码的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11759092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com