How to change the encoding of fileinput or fileoutput streams

R

Rohit Gupta

Hi,
Can we change the encoding in which the fileinputstream or
fileoutputstream reads or writes to a file. I need to manipulate the
text of a file, which is in unicode, in utf8 encoding, this thing works
fine on linux platform but when I try to run this on windows platform I
get garbage as the output because it processes it in the ANSI encoding.
Can someone suggest a way out of this, I desperately need a solution.

TIA

Rohit
 
L

Leon

Rohit Gupta said:
Hi,
Can we change the encoding in which the fileinputstream or
fileoutputstream reads or writes to a file. I need to manipulate the
text of a file, which is in unicode, in utf8 encoding, this thing works
fine on linux platform but when I try to run this on windows platform I
get garbage as the output because it processes it in the ANSI encoding.
Can someone suggest a way out of this, I desperately need a solution.

Perhaps better use:

PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));


BufferedReader in
= new BufferedReader(new FileReader("foo.in"));

Greetings, Leon.
 
R

Rohit Gupta

I dint get you, what shall the above code change in practical such that
the encoding problem gets resolved. Plz explain.
 
L

Leon

Rohit Gupta said:
I dint get you, what shall the above code change in practical such that
the encoding problem gets resolved. Plz explain.

If you write text, you better use classes which are appropriate, like
PrintWriter and BufferedReader.

Greetings, Leon.
 
T

Tris Orendorff

Leon said:
Perhaps better use:

PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));


BufferedReader in
= new BufferedReader(new FileReader("foo.in"));

Use OutputStreamWriter(out, "UTF8") and InputStreamReader(in, "UTF8") to specify encodings rather than
using the defaults, like FileWriter() and FileReader() do.

Sorry for any typos.

--

Sincerely,

Tris Orendorff
[Two antennae meet on a roof, fall in love and get married. The ceremony wasn't much, but the reception
was excellent.]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top