[...] in most cases, the method will be called using the default Charset,
Not really. Most of the time it will be called with an encoding in which you expect the file to be encoded. Usually these days it is UTF-8:
Files.readAllLines("fileName", StandardCharsets.UTF_8)
Your application can be run on multiple platforms and operating systems using different default encodings. You do not want your application to crash just because of this.
I think this is a good choice, fixing the wrong decisions from the past. Many older Java methods use the default system encoding, causing inconsistent behavior or an application, for example. between Windows and Linux. Forcing the choice of character encoding just makes your application more portable and secure.
By the way, since you mention the io.Source class - note that it returns an iterator instead of List<String> , as the Files class does. Advantage: the file is loaded lazily, not all at once until the huge ArrayList<String> . Disadvantage: you must close the source manually (which you cannot do in the code snippet).
source share