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 6531601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:53:51+00:00 2026-05-25T09:53:51+00:00

I’m messing about with Java, after a long hiatus, and I’m running into my

  • 0

I’m messing about with Java, after a long hiatus, and I’m running into my usual problems with its complexity.

I have what should be a simple problem, but what with InputStreams, InputReaders, and dealing with finally and close(), I’m not seeing an obvious answer.

I’m in the main() function of a simple command-line app. I’ve parsed my command-line, and I have a input filename. This contains either the path to a text file or “-“, which means that the program should read from System.in.

What I want is a String containing the contents of the file (or the remaining contents of System.in). And what I am ending up with is way too complex, so I’m looking for the simple way to do it.

Question 1: Given an InputStream (which might be System.in or it might be a FileInputStream() I opened myself, what’s the simplest way to read all of its contents into a String? The various readAll() methods I’ve been seeing returned byte arrays, for which there is a place, but it’s not what I am looking for.

Question 2: What is the usual idiom for making sure that any reader I open will always be closed?

If I were writing in C++, I’d have put the close() in my class’s destructor, and I’d be sure that close() would be called when my object fell out of scope.

If I were writing in C#, I’d have used a “using” block, and I’d have the same guarantee.

I’m sure this can be done in Java, but my attempts so far have seemed cumbersome.

If I try:

try
{
    InputStream inStream = new InputStream(...);
}
finally
{
    inStream.close();
}

I get complains that “inStream cannot be resolved”.

But if I try

InputStream inStream = null;
try
{
    inStream = new InputStream(...);
}
finally
{
    if (inStream != null)
        inStream.close();
}

I get “Unhandled exception type IOException.

So now I’m at:

InputStream inStream = null;
try
{
    inStream = new InputStream(...);
}
finally
{
    try
    {
        if (inStream != null)
            inStream.close();
    }
    catch (IOException ex)
    {
    }
}

And that just seems ludicrous.

What is the usual pattern?

  • 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-25T09:53:52+00:00Added an answer on May 25, 2026 at 9:53 am

    You might want to try using Apache Commons IO to wrap some of this stuff for you. An example:

    import org.apache.commons.io.FileUtils;
    
    File f = new File("mystuff.txt");
    String contents = FileUtils.readFileToString(f);
    

    Or, if you’re starting with a stream:

    import org.apache.commons.io.IOUtils;
    
    InputStream is = new InputStream(...);
    String contents = IOUtils.toString(is);
    

    And much, much more :-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

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.