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.
- The former allow you to consume (read) and produce (write) characters; i.e. textual data represented using Unicode code-points.
- The latter allow you to consume (read) and produce (write) bytes; i.e. binary or binary encoded data.
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.
Videos
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.
- The former allow you to consume (read) and produce (write) characters; i.e. textual data represented using Unicode code-points.
- The latter allow you to consume (read) and produce (write) bytes; i.e. binary or binary encoded data.
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.
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.