How to write chinese character to a text file
Hi experts,
I have been searching alot on this topic but none can solve my problem.
My problem is:
I have a string that contains both latin words and chinese characters that was input by the user.
I need to write this string to a text file. However, the chinese character always appear rubbish in my text file.
My code:
String text = "string containing latin and chinese character";
FileWriter fw = new FileWriter("abc.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(text);
bw.close();
Pls advise on how it should be done. Will appreciate if you can show me the code.
A million thanks!
[677 byte] By [
floza] at [2007-11-27 0:26:25]

# 2
Hi,
Thanks for the link. I tried but doesn't work. I'm not sure did I do it the correct way. This is what I did:
1: included <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"> in my jsp page
2: added the following lines right in the beginning of my jsp page before i request.getParameter the chinese word from the previous HTML file
String paramEncoding = application.getInitParameter("PARAMETER_ENCODING");
request.setCharacterEncoding(paramEncoding);
3: changed the encoding in WEB-INF\web.xml to
<?xml version="1.0" encoding="UTF-8"?>
4: included the following code in WEB-INF\web.xml
<context-param>
<param-name>PARAMETER_ENCODING</param-name>
<param-value>UTF-8</param-value>
</context-param>
5: I changed the encoding to Unicode (UTF-8) in my browser when inserting chinese character.
However, the chinese character that was write into the text file became ? now.
Pls advise. Will be really really appreciated! Thank you.
floza at 2007-7-11 22:24:58 >

# 6
Hi..
I have another question. So how should I do if I want to read a file that contains chinese word?
I used:
BufferedReader intemp = new BufferedReader(new FileReader("abc.txt"));
str = intemp.readLine())
abc.txt contains chinese word and after reading all the lines, I need to output to another file containing str. It gave me rubbish word..
A million thanks!
floza at 2007-7-11 22:24:58 >

# 7
Use InputStream/OutputStream (uses byte array) instead of Reader/Writer (uses char array). This gives you the possibility to encode the byte array streams.
Basic example:BufferedInputStream input = new BufferedInputStream(new FileInputStream(new File(path, name)));
ByteArrayOutputStream output = new ByteArrayOutputStream();
int length = input.available();
while (length-- > 0) {
output.write(input.read());
}
String content = output.toString("UTF-8");