/    Sign up×
Community /Pin to ProfileBookmark

How to save webpages without modifying their original charset?

I am writing a java project saving Chinese webpages.
My local OS default charset is gb2312, the Chinese national standard charset.
First I load the specified webpages to a StringBuffer, then flush the buffer to a specifed file.
Critical codes are below:

[CODE] public static StringBuffer webPage2Buffer(URL url,String encoding)
throws IOException
{
//String encoding = “UTF-8”;
StringBuffer result=new StringBuffer();
InputStream in=url.openStream();
BufferedReader buffRead = new BufferedReader(new InputStreamReader(in, encoding));
int c;
while((c=buffRead.read())!=-1) result.append((char) c);
return result;
}

public static void Buffer2File(StringBuffer strBuf,String writeTime, String storingPlace)
{
File rltFile = new File(storingPlace+writeTime+”.html”);
try
{
PrintWriter printer = new PrintWriter(rltFile);
printer.println(strBuf.toString());
printer.close();
}
catch (IOException e1)
{
e1.printStackTrace();
};
}[/CODE]

The webpages are originally encoded in UTF-8, but after being saved, they are transferred to gb2312,
which is not wanted. What should I do to save them without modifying the original charset?
Thanks!?

to post a comment
Java

1 Comments(s)

Copy linkTweet thisAlerts:
@aaron792authorApr 05.2008 — Solved.

[CODE] PrintWriter printer = new PrintWriter( new OutputStreamWriter( new FileOutputStream(rltFile), "UTF-8"));[/CODE]
Hope can help!
×

Success!

Help @aaron792 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...