Read the entire input as a String using Scanner

Download java eBook

Example

You can use Scanner to read all of the text in the input as a String, by using \Z (entire input) as the delimiter. For example, this can be used to read all text in a text file in one line:

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);

Remember that you'll have to close the Scanner, as well as catch the IoException this may throw, as described in the example Reading file input using Scanner.

Stats

Contributors: 1
2016-04-22
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook