Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Read Text from Standard IO 
 

In this section, you will see how the standard I/O is used to input any thing by the keyboard or a file.

 

Reading Text from the Standard Input

                         

Standard Streams:

Standard Streams are a feature provided by many operating systems. By default, they read input from the keyboard and write output to the display. They also support I/O operations on files.

 Java also supports three Standard Streams:

  • Standard Input: Accessed through System.in which is used to read input from the keyboard.
  • Standard Output: Accessed through System.out  which is used to write output to be display.
  • Standard Error: Accessed through System.err which is used to write error output to be display.

These objects are defined automatically and do not need to be opened explicitly. Standard Output and Standard Error, both are to write output; having error output separately so that the user may read error messages efficiently.
System.in is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap System.in within the InputStreamReader as an argument.

InputStreamReader inp = new InputStreamReader(system.in);

Working with Reader classes:

Java provides the standard I/O facilities for reading text from either the file or the keyboard on the command line. The Reader class is used for this purpose that is available in the java.io package. It acts as an abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). the Reader class is further categorized into the subclasses. 

The following diagram shows a class-hierarchy of the java.io.Reader class.

However, most subclasses override some of the methods in order to provide higher efficiency, additional functionality, or both.

InputStreamReader:

An InputStreamReader is a bridge from byte streams to character streams i.e. it reads bytes and decodes them into Unicode characters according to a particular platform. Thus, this class reads characters from a byte input stream. When you create an InputStreamReader, you specify an InputStream from which, the InputStreamReader reads the bytes.

The syntax of InputStreamReader is written as:

InputStreamReader <variable_name> = new InputStreamReader(system.in)

BufferedReader : 

The BufferedReader class is the subclass of the Reader class. It reads character-input stream data from a memory area known as a buffer maintains state.  The buffer size may be specified, or the default size may be used that is large enough for text reading purposes. 
BufferedReader
converts an unbuffered stream into a buffered stream using the wrapping expression, where the unbuffered stream object is passed to the constructor for a buffered stream class.

For example the constructors of the BufferedReader class shown as:

BufferedReader(Reader in):Creates a buffering character-input stream that uses a default-sized input buffer.

BufferedReader(Reader in, int sz): Creates a buffering character-input stream that uses an input buffer of the specified size.

BufferedReader class provides some standard methods to perform specific reading operations shown in the table. All methods throws an  IOException, if an I/O error occurs.

 Method  Return Type  Description
 read( )  int  Reads a single character 
 read(char[] cbuf, int off, int len)  int  Read characters into a portion of an array.
 readLine( )  String  Read a line of text. A line is considered to be  terminated by ('\n').
  close( )  void   Closes the opened stream.

  This program illustrates you how to use standard input stream to read the user input..

import java.io.*;

 
public class ReadStandardIO{
 
  
public static void main(String[] argsthrows IOException{

      InputStreamReader inp = new InputStreamReader(System.in)
      BufferedReader br = new BufferedReader(inp);

      System.out.println("Enter text : ");
 
     
String str = in.readLine();


     System.out.println("You entered String : ");

      System.out.println(str);
  }
}

 

Output of the Program:

C:\nisha>javac ReadStandardIO.java

C:\nisha>java ReadStandardIO
Enter text :
this is an Input Stream
You entered String :
this is an Input Stream

C:\nisha>

Download this example

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

9 comments so far (
post your own) View All Comments Latest 10 Comments:

String str = in.readLine();

in.readLine() is not valid.

it should be
br.readLine();

Posted by Nageshwar Rao.Aaguri on Monday, 12.1.08 @ 05:14am | #82231

Shouldin it be String str = br.readLine();
instead of String str = in.readLine();?

Posted by Martin blue on Sunday, 07.6.08 @ 19:25pm | #65981

write an application that writes the first 15 numbers of fibonacci series to file . A second APPLICATION that reads this data from a file dispays it .

Posted by amit on Saturday, 10.6.07 @ 13:38pm | #31954

No errors found.. Its a great help to beginners who are finding solutions to their programming problems...

Posted by Anoj on Monday, 08.6.07 @ 13:15pm | #22745

I got an exception as well so I tried changing the variable "in" to "cin" and then my program built just fine.

Posted by Barbara on Thursday, 03.29.07 @ 05:15am | #12879

MARVELAS

Posted by Srinivas on Friday, 03.23.07 @ 10:04am | #12535

eXCELLENT

Posted by balachandra on Friday, 03.2.07 @ 15:50pm | #10228

Super

Posted by Ravisankar on Monday, 02.19.07 @ 13:09pm | #8367

I tried this code,but found an exception.

Posted by Tiji on Friday, 12.29.06 @ 10:27am | #2193

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.