Chathuri Gunawardhana
Chathuri Gunawardhana

Reputation: 181

Reading non textual content and write it back to the file

I need to read the line <row>⠁⠇⠕⠝⠛⠀⠺⠊⠞⠓⠀⠍⠁⠞⠓⠑⠍⠁⠞⠊⠉⠁⠇⠀⠉⠕⠝⠞⠑⠝⠞</row> from a file and then write back. But when I write back content in file is

<row>�⠃⠕⠧⠑⠀⠙⠑�⠕⠞⠑⠀�⠀⠊�⠞⠑⠗⠛⠗�⠞⠊⠕�</row>

Can you please help me to fix it? I need to do this in java

Thanks!

Upvotes: 0

Views: 1025

Answers (3)

Marko Topolnik
Marko Topolnik

Reputation: 200226

This is clearly a text encoding issue. The problem may be happening either while reading or while writing, or while reading again what was written. Your output looks like misinterpreted little-endian UTF-16 (except for the tags that look like plain ASCII).

BTW your "non-textual" remark is just misleading -- any character data is textual, these are synonyms.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502696

EDIT: Now we've got the <row> part, it looks like it is meant to be text data after all. However, you've given us no information about what produced this file, or what the data between the <row> tags is meant to be. Is it text, or is it binary data and somehow you're meant to shift from reading the text of <row> to reading the binary data? How are you meant to know where the <row> ends?

You've got to understand the meaning and format of your data - we can't do that for you. If you can explain the meaning and format, we can help you turn that into code... but we can't help much without that information.


You talk about reading a "line" - but then you give non-text data. There's no such concept as a "line" within binary data.

If you're currently using a Reader of some description (e.g. FileReader) - don't. They're designed for text data. Trying to treat binary data as text is almost guaranteed to lose information.

Read with an InputStream, write with an OutputStream, and all should be well (assuming you use them correctly, of course - in particular, use the return value from InputStream.read appropriately).

Upvotes: 0

Luxspes
Luxspes

Reputation: 6760

You need to use a binary stream instead of a character stream.

In other words you are doing this: http://docs.oracle.com/javase/tutorial/essential/io/charstreams.html

But you should be doing this: http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html

Upvotes: 0

Related Questions