JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Get Best Tutorial on all major programing language

Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Java I/O – The Basics

  • Java I/O is based around the concept of a stream Ordered sequence of information (bytes)
    coming from a source, or going to a ‘sink’
  • Simplest stream reads/writes only a single byte, or an array of bytes at a time
  • Designed to be platform-independent
  • The stream concept is very generic Can be applied to many different types of I/O
    Files, Network, Memory, Processes, etc

  • The java.io package contains all of the I/O classes.
    •  Many classes specialised for particular kinds of stream operations, e.g. file I/O
    • Reading/writing single bytes is quite limited So, it includes classes which provide
      extra functionality
      e.g. buffering, reading numbers and Strings (not bytes), etc.
  • Results in large inheritance hierarchy, with separate trees for input and output stream classes

Input and Output

  • Console
    Scanner
    System.out
  • Command line parameters
  • File
    Reader and Writer
    InputStream and OutputStream
    Scanner
  • GUI
Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes
data from a source (keyboard, file, etc.)
– it acts as a buffer between the data source and destination

•    Input stream: a stream that provides input to a program
– System.in is an input stream

•    Output stream: a stream that accepts output from a program
– System.out is an output stream

• A stream connects a program to an I/O object
– System.out connects a program to the screen
– System.in connects a program to the keyboard


Stream Types

Two Types of Stream Classes:

  • Byte Stream
    • Usually works for bytes & binary objects
    • InputStream and OutputStream are the abstract classes which represent Byte Streams
  • Character Stream
    • Usually works for Characters & Strings
    • Follows the Unicode
    • Introduced to cater to the needs of internationalization
    • Reader and Writer are the abstract classes which represents Character Streams




FileOutputStream & FileInputStream classes:
  • These are sub classes of OutputStream and InputStream classes respectively
  • Used to write & read binary data and /or binary object to and from the data source
  • FileInputStream object is used to READ data from the file
  • FileOutputStream object is used to WRITE data to  the file
FileOutputStream fos = new FileOutputStream(“abc.txt”);
FileInputStream fis = new FileInputStream(“abc.txt”);



BufferedInputStream & BufferedOuputStream classes:
  • Subclasses of InputStream & OutputStream classes respectively
  • We can wrap a BufferedInputStream around the FileInputStream for reading &
    storing large chunks of data in a buffer at once for later use




import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class myfile3 {
  public static void main(String[] args) {
    try {
      File file2 = new File("c:\\myhcl2\\ss.txt");
      
      FileInputStream fin = new
FileInputStream(file2);
            BufferedInputStream buf1=new
 
BufferedInputStream(fin);
             
      int i = 0;
      while (i != -1) {
        i = buf1.read();
        if(i!=-1)
        System.out.print((chari);

      }

    catch (Exception e) {
      System.out.println(e);
    }

  }
}
Output:

javac myfile3.java
java myfile3

Download  code

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class myfile4 {
public static void main(String[] args) {
  try{
  File file2=new File("c:\\myhcl2\\ss.txt");
  FileOutputStream fout=new FileOutputStream(file2);
  BufferedOutputStream buf=new BufferedOutputStream(fout);
  byte aa[]="java is the most
used language in the world"
.getBytes();
  //fout.write(aa);
  
  buf.write(aa);
  buf.flush();
  
  System.out.println("file is written");
  }
  catch (Exception e) {
    // TODO: handle exception
  }


}
}
Output:-

javac myfile4.java
java myfile4
file is written
Download code

DataInputStream & DataOutputStream classes:

We can write primitive data type through DataInputStream and DataOutputstream Classes.

Character Streams
  • Reader
  • Writer
FileReader and FileWriter classes
  • These classes are the Subclasses of Reader & Writer class
  • Reader class object  is used to READ character dtaa  from the file
  • Writer class object  is used to WRITE character data to  the file
BufferedReader & BufferedWriter classes:
  • These classes Provides buffering to Character streams
  • these classes extends the  Reader & Writer classes
  • BufferedReader is used to for reading purpose.(reading from console or from file)

InputStreamReader class:
  • it is used as a wrapper for any InputStream object
  • Converts the bytes data  to Unicode characters.


Example

import java.io.File;
import java.io.FileReader;

public class myfile5 {
  public static void main(String[] args) {
    try {
      File file2 = new File("c:\\myhcl2\\ss.txt");

      FileReader frd=new FileReader(file2);

      int i = 0;
      while (i != -1) {
        i = frd.read();
        if (i != -1)
          System.out.print((chari);

      }

    catch (Exception e) {
      System.out.println(e);
    }

  }
}
Output:
javac myfile5.java
java myfile5
java is the most used language in the world

Download Code

Example

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class myfile6 {
  public static void main(String[] args) {
    try {

      FileReader frd=new FileReader("c:\\myhcl2\\ss.txt");
            BufferedReader br=new BufferedReader(frd);
            
      int i = 0;
      while (i != -1) {
        i = br.read();
        if (i != -1)
          System.out.print((chari);

      }

    catch (Exception e) {
      System.out.println(e);
    }

  }
}
Output:=
javac myfile6.java
java myfile6
java is the most used language in the world

Download Code

Example:

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

public class myfile7 {
  public static void main(String[] args) {
    try {

      FileWriter fw = new FileWriter("c:\\myhcl2\\ss.txt");
      BufferedWriter bw=new BufferedWriter(fw);
      String a = "we all are indian";

      bw.write(a);
      bw.flush();
      System.out.println("file is written");
    catch (Exception e) {
      System.out.println(e.getMessage());
    }

  }
}

Output:-
javac myfile7.java
java myfile7
file is written

Download Code

Example:-

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class myfile8 {
  public static void main(String[] args) {
    String a;
    try {

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

      System.out.println("enter name");
      String name = bf.readLine();

      System.out.println("enter no1");
      a = bf.readLine();
      int x = Integer.parseInt(a);

      System.out.println("enter no2");
      a = bf.readLine();
      int y = Integer.parseInt(a);
      int z = x + y;
      System.out.println("sum of " + x + "and " + y + " is " + z);
      System.out.println("my name is " + name);
    catch (Exception e) {
      System.out.println(e.getMessage());
    }

  }
}
Output:-
javac myfile8.java
java myfile8
enter name
deepak
enter no1
90
enter no2
92
sum of 90and 92 is 182
my name is deepak
Download Code