Skip to content

Latest commit

 

History

History
8 lines (6 loc) · 351 Bytes

java-read-string-from-file.md

File metadata and controls

8 lines (6 loc) · 351 Bytes

Title: Java: Read a file as a string using Scanner Tags: java|java-io

If you first open a Scanner using a file location wrapped in a FileReader, you can then issue hasNext() and nextLine() to read from that file:

Scanner in = new Scanner(new FileReader("file.txt"));
String s, str="";
while(in.hasNext() && (s=in.nextLine())!=null) str+=s+"\n";