Reputation: 83
Not sure I am understanding this correctly, but I was thinking that when I need to store a file content as a byte array in Java, I need to provide Java with the encoding of the file.
If I want to do it with a ready for example, I'll need to do something like:
Reader reader = Files.newBufferedReader(myFilePath, StandardCharsets.ISO_8859_1)
Why can't I specify the encoding the the file when using Files.readAllBytes(myFilePath)
?
I'd like to do something like Files.readAllBytes(myFilePath, StandardCharsets.ISO_8859_1)
.
Am I getting this wrong?
Upvotes: 3
Views: 7286
Reputation: 719307
Yes you are rather getting it wrong. But it easy to get it wrong ... if you don't understand the Java way of modeling textual data.
You are conflating the functionality of Reader / Writer with that of InputStream / OutputStream.
Now many Reader
and Writer
classes actually perform two functions at the same time; i.e. sourcing or sinking the data AND converting the data to / from characters. In some cases, you can tell the class how to do the conversion step by supplying an encoding.
By contrast an InputStream
or OutputStream
is a source or sink for bytes, and typically1 doesn't do any character translation.
In the case of Files.readAllBytes
, the method is reading all of the bytes from a file and putting them into a byte array. It doesn't take a Charset
parameter because it is not designed to do any decoding.
On the other hand, Files.readAllLines
does take a Charset
. That is because it is delivering the file content as an array of String
objects representing the lines of the file. To when converting bytes to a Java String
you need to say how the text / characters represented by the bytes are encoded.
1 - Hypothetically it could ... but you will see that Java doesn't provide classes like ReaderInputStream
or WriterOutputStream
. There is little need for them.
Upvotes: 6
Reputation: 3311
No, you only need a Charset
if you want represent the bytes as a String
(i.e. as text) because the meaning of the bytes depends on the encoding.
byte[] bytes = Files.readAllBytes(Paths.get("/temp.txt"));
String text = new String(bytes, StandardCharsets.ISO_8859_1);
A single byte array can represent multiple different strings depending on the encoding and the bytes in the array are independent of the encoding, so it doesn't make sense to be able to pass the encoding as an argument to this function.
Upvotes: 3