Java Language Read the entire input as a String using Scanner

Download Java Language for free


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.

Reading file input using Scanner