Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 972117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:04:17+00:00 2026-05-16T03:04:17+00:00

I am trying to read a simple text file into a String. Of course

  • 0

I am trying to read a simple text file into a String. Of course there is the usual way of getting the input stream and iterating with readLine() and reading contents into String.

Having done this hundreds of times in past, I just wondered how can I do this in minimum lines of code? Isn’t there something in java like String fileContents = XXX.readFile(myFile/*File*/) .. rather anything that looks as simple as this?

I know there are libraries like Apache Commons IO which provide such simplifications or even I can write a simple Util class to do this. But all that I wonder is – this is a so frequent operation that everyone needs then why doesn’t Java provide such simple function? Isn’t there really a single method somewhere to read a file into string with some default or specified encoding?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:04:18+00:00Added an answer on May 16, 2026 at 3:04 am

    Yes, you can do this in one line (though for robust IOException handling you wouldn’t want to).

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

    This uses a java.util.Scanner, telling it to delimit the input with \Z, which is the end of the string anchor. This ultimately makes the input have one actual token, which is the entire file, so it can be read with one call to next().

    There is a constructor that takes a File and a String charSetName (among many other overloads). These two constructor may throw FileNotFoundException, but like all Scanner methods, no IOException can be thrown beyond these constructors.

    You can query the Scanner itself through the ioException() method if an IOException occurred or not. You may also want to explicitly close() the Scanner after you read the content, so perhaps storing the Scanner reference in a local variable is best.

    See also

    • Java Tutorials – I/O Essentials – Scanning and formatting

    Related questions

    • Validating input using java.util.Scanner – has many examples of more typical usage

    Third-party library options

    For completeness, these are some really good options if you have these very reputable and highly useful third party libraries:

    Guava

    com.google.common.io.Files contains many useful methods. The pertinent ones here are:

    • String toString(File, Charset)
      • Using the given character set, reads all characters from a file into a String
    • List<String> readLines(File, Charset)
      • … reads all of the lines from a file into a List<String>, one entry per line

    Apache Commons/IO

    org.apache.commons.io.IOUtils also offer similar functionality:

    • String toString(InputStream, String encoding)
      • Using the specified character encoding, gets the contents of an InputStream as a String
    • List readLines(InputStream, String encoding)
      • … as a (raw) List of String, one entry per line

    Related questions

    • Most useful free third party Java libraries (deleted)?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.