Java: Read a file as a string using Scanner

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";
java java-io

Edit on github
comments powered by Disqus
Click me