IO package In Java

Java Input Output Projects

Java I/O Project 1

Java Input Output Examples

Java Input Output Examples

FILE HANDLING (STREAM)

All the Java Program can be pass through Input/Output (I/O) streams. A stream is an abstraction that either Consumer or Produces information, linked to a physical device by the Java I/O system. In java an object from which, read and write a sequence of bytes is called an input stream and an output stream respectively, specified in the abstract classes InputStream and OutputStream. Streams also support different kind of data, including simple bytes, primitive data types, localized characters, and objects.

The byte-oriented streams are inconvenient/ difficult for processing information stored in Unicode (recall that Unicode uses two bytes per character), there is a separate hierarchy of classes for processing Unicode characters that inherit from the abstract Reader and Writer classes. These classes have read and write operations that are based on 2-byte Unicode characters rather than on single-byte characters

IO Tutorials and Interview Questions
Overview of I/O within the Java

Java I/O can be summarized as follows

  1. In Java two kinds of stream use- 1) 8-bits stream introduce in Java 1.0 version, and 2) 16-bits stream introduce in Java 1.1 version, to over come limitation like did not support internationalized character sets (e.g Hindi, Korean , Chinese, Japanese, etc).
  2. The 8-bits Stream are types of InputStream or OutputStream and 16-bits Stream are types of Reader (input) and Writer (output).
  3. All I/O file in java is passing through using stream.
  4. Each stream offer one types of I/O service ( e.g. Binary I/O, Character I/O, byte array I/O, file I/O, etc).
Byte Streams and Character Streams

In java language, two type of stream is define- byte and character streams

  • Byte Streams: Byte Streams provide a standard/ convenient means for handling input or output (I/O) of streams. Generally, used for writing or reading of binary data.
  • Character Streams: Character Streams provide a standard/ convenient means for handling input and output (I/O) of characters. Generally, used for handle Unicode (encode data) data and in some case, character streams are more efficient than byte streams.
Bridging the gap between Byte and character stream

Stream classes java.io.InputStreamReader and java.io.OutputStreamWriter able to provide the bridge to the gap between them. The main purpose of these classes is to convert the byte data into character data according to the specific encoding or platform default.

 

/*
* Syntax for Bridging the gap between the Byte and character stream
*/

package r4r.co.in;

class r4r 
   {
public static void main(String
[] args) 
{
     BufferedReader reader = new 
     BufferedReader(new 
     InputStreamReader(System
     .in));
     System.out.println("
     Insert input String!");
     String string = reader.readLine();
}
   }
Streams classes

Streams I/O classes are belongs to java.io package and are of two types such as

  1. Top level abstract classes: Abstract Reader and Writer classes are parent classes for character-stream based classes in the java.io package. Reader classes are used to read 16-bit character streams and Writer classes are also used to write to 16-bit character streams. java.io.Reader and java.io.Writer are top level classes contain following method for reading and writing to streams.
  2. Method of java.io.Reader/ InputStream

    • abstract int read(): use for reading the next byte of data from the input stream.
    • int read(char cbuf[]): use for reading the bytes from the stream and stores them in cbuf[] array .
    • int read(char cbuf[]: int offset, int length)-use for reading upto length bytes of data from the input stream into an array of bytes.
    • void close(): use for closes this input stream and releases all the system resources that associated with the stream.

    Method of java.io.Writer /OutputStream

    • int write(int c): Use for write single Method
    • int write(char cbuf[]): Use for writes cbuf[] from the specified byte array to this output stream.
    • int write(char cbuf[]: int offset, int length)-Use for writes length bytes from the specified byte array starting at offset to this output stream.
    • void flush(): use for flushes this output stream and forces any buffered output bytes to be written out.
    • void close(): use for closes this input stream and releases all the system resources that associated with the stream.
  3. Descendant implementation classes: Specialized Descendent Stream Classes are the subclass of Top level classes, provide additional functionality. All the Specialized classes is extend their parent classes.
  4. Use the syntax, the BufferedReader class not only provides buffered reading for efficiency but also provides methods such as 'readLine()' to read a line of input in serialization manner.

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String string = reader.readLine();
    

    The Specialized classes also belongs to java.io package like

    • BufferedReader
    • FileReader
    • StringReader
    • LineNumberReader
    • FilterReader
    • PushbackReader
    • InputStreamReader

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: