Does Java has a one line instruction to read to a text file, like what C# has?
I mean, is there something equivalent to this in Java?:
String data =
Java 11 adds support for this use-case with Files.readString, sample code:
Files.readString(Path.of("/your/directory/path/file.txt"));
Before Java 11, typical approach with standard libraries would be something like this:
public static String readStream(InputStream is) {
StringBuilder sb = new StringBuilder(512);
try {
Reader r = new InputStreamReader(is, "UTF-8");
int c = 0;
while ((c = r.read()) != -1) {
sb.append((char) c);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
Notes: