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

  • Home
  • SEARCH
  • 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 7870657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:40:52+00:00 2026-06-03T01:40:52+00:00

I am using the code below to read a sql statement from stdin on

  • 0

I am using the code below to read a sql statement from stdin on the command line:

    BufferedReader in = null;
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
        in = new BufferedReader(new InputStreamReader(System.in));
        while((line = in.readLine()) != null) {
            sb.append(line);
        }
    } finally {
        if (in!=null) in.close();
    }

My problem is that the application needs to run sometimes with data from stdin, and sometimes not (no piped input). If there is no input in the above code, in.readLine() blocks. Is there a way to rewrite this code so that it can still run if nothing is piped in?

UPDATE: The application is designed to expect piped data from the command line, not from the keyboard.

  • 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-06-03T01:40:53+00:00Added an answer on June 3, 2026 at 1:40 am

    I don’t think there is any way to check if you will eventually get another line of input. Note that your current code does terminate if the user closes the input stream (e.g., with ^D in a terminal).

    BufferedReader.ready() checks if there is some data on the stream. Like T.J. mentioned, you might be unlucky and ask for data right before you actually receive it, and your user will be sad because you didn’t answer their query.

    Scanner.hasNextLine() is a blocking operation, so probably not what you’re looking for.

    You could have the user specify whether or not to read from System.in, for instance by using command line arguments.

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

Sidebar

Related Questions

I'm using the code below to read tables and views from a SQL database,
In the below code, using (SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection)) { while (dr.Read()) { _emailTemplate.EmailContent
Using the code below (from a console app I've cobbled together), I add seven
I'm trying to read an Excel (xlsx) file using the code shown below. I
I'm calling the code below. On the line (IDataReader dr = cmd.ExecuteReader()) sql barfs
I am using the code below to read a ~2.5Gb Xml file as fast
I'm using code below, to launch bash command, with Admin Privileges. I need to
Using the code below, I am attempting to fill a Canvas with UIElements and
Using the code below, I want to keep the same menu div opened in
Using the code below I'm expecting a green rectangle and a red one of

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.