Friday, August 11, 2017

Basic IO


Java I/O (Input and Output) is used to process the input and produce the output.

It uses the concept of streams to make I/O operation fast. The java.io package contains all the classes required for input and output operations.

Stream

A stream is a sequence of data. In Java a stream is composed of bytes.

In java,3 streams are created for us automatically. All these streams are attached with console.



1) System.out: standard output stream

2) System.in: standard input stream

3) System.err: standard error stream



Let's see the code to print output and error message to the console.

System.out.println("simple message");  

System.err.println("error message");  



Let's see the code to get input from console.



int i=System.in.read();//returns ASCII code of 1st character  

System.out.println((char)i);//will print the character  


Output Stream vs Input Stream



OutputStream

Java application uses an output stream to write data to a destination, it may be a file, an array, peripheral device or socket.

InputStream

Java application uses an input stream to read data from a source, it may be a file, an array, peripheral device or socket.

Reading Console Input



In Java, there are 3 ways to read input from a console.

  1. BufferedReader + InputStreamReader (Classic)
  2. Scanner (JDK 1.5)
  3. System.console (JDK 1.6)





1. BufferedReader + InputStreamReader

InputStreamReader  that can read data from the keyboard.



Syntax

InputSteamReader obj = new InputStreamReader (System.in);



Connect InputStreamReader to BufferReader, which is another input type of stream to read data properly.

Syntax

BufferedReader br = new BufferedReader (obj);

two steps can be combined and rewritten in a single statement

Syntax

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

Now, we can read the data coming from the keyboard using read () and readLine () methods available in BufferedReader class.
2. Scanner Class
In JDK 1.5,the developer starts to use java.util.Scanner to read system input.
Syntax
Scanner scanner = new Scanner(System.in);

3. System.console

In JDK 1.6, the developer starts to switch to the more simple and powerful java.io.Console class to read system input.
 It provides methods to read texts and passwords.
syntax
Console c=System.console();  

 Writing Console Output

PrintStream is an output stream derived from OutputStream,it also implements the low-level method write().
write() can be used to write to the console.
Syntax
The simplest form of write() defined by the PrintStream  is
void write(int byteval)

Naming conventions

All the classes, interfaces, packages, methods and fields of java programming language are given according to java naming convention.
      class name should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
      interface name should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
      method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
      variable name should start with lowercase letter e.g. firstName, orderNumber etc.
      package name should be in lowercase letter e.g. java, lang, sql, util etc.
      constants name should be in all uppercase letters. e.g. RED, YELLOW, MAX_PRIORITY etc.
       

No comments:

Post a Comment