Read the entire input as a String using Scanner

Download java eBook


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();

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.


Contributors: 1
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial:

Download eBook