No, that's definitely not the way to do it:
- You're converting the bytes into text as you read them from a file
- You're removing all line-breaks (they're not returned from
readLine
)
- You're converting the text back into bytes
You're very likely to lose data this way.
To load bytes you shouldn't be using a Reader
at all - just use InputStream
. It's very important to understand that binary data and text data aren't the same. Treating either of them as the other is a really bad idea.
If you just want to read all the data from an file I would personally use Guava and its Files
class:
byte[] data = Files.toByteArray(file);
Or if you've already got an InputStream
, use ByteStreams
:
byte[] data = ByteStreams.toByteArray(inputSTream);
This also works with InputSupplier
.