views:

500

answers:

2

i parsed a text (CAL code) with BufferedReader and BufferedWriter in Java, unfortunately, lines which i red and wrote with outStream.write(line); have changed, please look at Screenshots:

http://uploadz.eu/images/4qz8mtkm2d9zx3x5ms3n.png h**p://uploadz.eu/images/c03hgkrgrmit2ij2mug.png

as you see, some special character did changed the lines although i intended NOT to change them.

as far as i know, Bufferedwriter / Reader should work in unicode by default.

+2  A: 

Well BufferedWriter and BufferedReader are encoding agnostic - they never deal with the actual encodings, as they're just buffering existing readers and writers.

Now FileWriter and FileReader use the default system encoding (urgh). To work round this, you should usually use an InputStream / InputStreamReader or OutputStream / OutputStreamWriter pair (possibly wrapped in a BufferedReader / BufferedWriter), and specify the encoding explicitly.

You haven't said what you're actually reading from - is it a file? Do you know the encoding of the file?

Jon Skeet
BufferedWriter df = new BufferedWriter(new OutputStreamWriter("out.txt"));does not work.it is a .txt file. unfortunately I don't know the encoding of the file.
dayscott
i found out: it should be ANSI. i'd appreciate further suggestions.
dayscott
"ANSI" isn't one single encoding. It can mean a variety of different encodings, usually 8 bit extensions to ASCII.
Jon Skeet
@dayscott: you can't just specify the filename for OutputStreamWriter. The constructor for OutputStreamWriter takes an OutputStream as its parameter. So replace "out.txt" with: new FileOutputStream("out.txt"). You might also find http://stackoverflow.com/questions/499010 to be useful
barrowc
A: 

You should either know the encoding if you're going to treat the content as text (String), or, when copying is your goal, or treat the content as a byte array.

extraneon
firefox: http://uploadz.eu/images/7anvv8hok4bdt2ewylt1.jpgjava compiler: "Cp1252"i googled that "Cp1252" seems to be the java "standard" charset.
dayscott
wordStream = new FileInputStream( "x.txt" ); InputStreamReader input = null; input = new java.io.InputStreamReader( wordStream, "Cp1252" ); reader = new BufferedReader(input); String str; try { while((str = reader.readLine()) != null){ System.out.println(str); }unfortunately the output is still wrong encoded.
dayscott
Problem solved ! simply by using the RandomAccessFile class. I used readLine and writeBytes. As a separator I used System.getProperty("line.separator") - running Win XP. thx for the support guys :)
dayscott