nitishagar
nitishagar

Reputation: 9413

difference between bytes returned from reading a file and getbytes from string in Java?

Reading file directly into byte array gives a different output compared to reading data into a String and then getting bytes from it.

What is the form for the bytes read directly from a file and how is it different from the get bytes in String.

Upvotes: 1

Views: 1967

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500835

Reading file directly into byte array gives a different output compared to reading data into a String and then getting bytes from it.

Well, it might. And it might not. It depends on how you've read the file as text, and how you've converted the text back into bytes.

If you use the same encoding in both directions and the file originally contained text in that encoding, then you're likely to get the same bytes back. But if you use the wrong encoding (e.g. you read ISO-8859-1-encoded text as UTF-8) or if you use different encodings for the two conversions, then you're very likely to get back different results.

Think of text as being a bit like an image format - if you read a .png file and then write out a .jpeg file, you wouldn't expect that to have the same bytes, would you? Likewise if you tried to read a .png file with a JPEG decoder, you'd expect to get garbage out (or more likely an error).

Basically, don't think of text as a sequence of bytes - it's not. Think of it as being entirely separate, with encodings used to convert between text and binary representations. See Marc Gravell's blog post on IO for more details.

Upvotes: 3

Related Questions