Quantcast
Channel: How do I create a Java string from the contents of a file? - Stack Overflow
Viewing all articles
Browse latest Browse all 36

Answer by Johan Esteban Cañas Ossa for How do I create a Java string from the contents of a file?

$
0
0

Using BufferedReader as mentioned in comments before but this way is more readable:

String FILE_PATH = "filepath.txt";try (FileReader fileReader = new FileReader(FILE_PATH)) {    BufferedReader fileBufferReader = new BufferedReader(fileReader);    String text = fileBufferReader.lines()        .collect(Collectors.joining(System.lineSeparator()));    System.out.println(text);} catch (IOException e) {    // exception handling}

Viewing all articles
Browse latest Browse all 36

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>