IOSTREAMS

In java language stream represents flow of data.Whenever data is tranfered from input device to java program is known as input stream and if the data is transfered from java program to output device is known as output stream.
In java language IO streams concept was introduced to handle input and output flow of data(especially with respective to files).

File:
File is a collection of information used to store data permanently.Every programming language bydefault supports temparary memory.But we want to store input or output data permanently it is recamended to used either file system or database.

Operation on files:
  1. Open file
  2. Writing data into file
  3. Reading data from file
  4. Close file
To perform above operations on file through java program some predefined classes are available in java.io package.These classes mainly catagerized into two types.
  • ByteStream classes
  • CharacterStream classes

ByteStream classes

These are set of classes used to write or read the data from or into the files in the form of binary values these are catagerized into following types.
  • Input stream classes
    • FileInputStream
    • DataInputStream
    • SequenceInputStream
    • ByteArrayInputStream
    • BufferedInputStream
    • ObjectInputStream
    Note:Used to read the data from the files in the form of binary values.
  • Output stream classes
    • FileOutputStream
    • DataOutputStream
    • ByteArrayOutputStream
    • BufferedOutputStream
    • ObjectOutputStream
    Note:Used to write the data into files in the form of binary values.

CharacterStream classes

Thsese are the set of classes used to perform either writing or reading operation from the files or into the files in the form of text.These are classified into following types.
  • Reader classes
    • FileReader
    • CharacterArray classes
    • Buffered Reader classes
  • Writer classes
  • Note:Used to read the data from a file in the form of text.
    • file writer
    • character array writer
    • buffered writer
    Note:Used to write the data from file in the form of text.

OutputStream Classes:

for all outputstream classes outpustream is predefined super class by using these classes we can able to write the data into files.

FileOutputStream:

The main aim of tu=his class is open the given file in a writing mode.
syntax:
FileOutputStream fos=new FileOutputStream("File path");
If the file is available in the same location where the java program is accessble them directly filename with extension can be written otherwise complete path of the file should be written.
example: FileOutputStream fos=new FileOutputStream(abc.txt);
char x='A';
fos.write(c); -- used to write a single character into file
Note:
  • Using looping statements character by character can be written into a file.

  • import java.io.*;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
          
            FileOutputStream fos=new FileOutputStream("abc.txt");
            fos.write(66);
    
        }
    
    }                
                

    DataOutputStream:

    If we want to write a text value or statements into file at a time then it's recamended to use dataoutputstream class.
    DataOutputStream dos=new DataOutputStream(fos);

    String s="Hello msk Tutorials";
    dos.writebytes(s); -- used to write a statements or string at a time into file.
    import java.io.*;
    import java.util.*;
    public class Main {
    
        public static void main(String[] args) throws IOException {
            Scanner s=new Scanner(System.in);
            System.out.print("Enetr String");
            String st=s.next();
            FileOutputStream fos=new FileOutputStream("abc.txt");
            DataOutputStream dos=new DataOutputStream(fos);
            dos.writeBytes(st);
    
        }
    
    }
    
    In the above Example writeBytes method is write the data into file.
    main throws an IOException that we can handled by using try-catch construction or that can throw from the JVM with the help of throws keyword.

    ByteArrayOutputStream:

    Which can be used to write the data of an array into file.
    FileOutputStream fos=new FileOutputStream(abc.txt);
    BytearrayOutputStream bos=new BytearrayOutputStream(fos);
                
                byte b[];
                ...
                ... logic
                ...
                bos.write(b);
                
    import java.io.*;  
     class Main {  
    public static void main(String args[])throws Exception{    
          FileOutputStream fout1=new FileOutputStream("abc.txt");    
          
            
          ByteArrayOutputStream bout=new ByteArrayOutputStream();  
          for(int x=1;x<=100;x++)
          {
          bout.write(x); 
          }
          bout.writeTo(fout1);    
          
        
          bout.close();
          System.out.println("Success...");    
         }    
        }  
        
        
    
    Using ByteArrayOutputStream data can be transfer to a file with a faster rate.

    BufferedInputStream:

    It is predefined class in java language.Whis is used to tranfer the data into file.
    FileOutputStream fout = new FileOutputStream("abc.txt"); 
    BufferedOutputStream bout = new BufferedOutputStream(fout); 
                
    import java.io.*;  
     class Main {  
    public static void main(String args[])throws Exception{    
         FileOutputStream fout = new FileOutputStream("abc.txt"); 
         BufferedOutputStream bout = new BufferedOutputStream(fout); 
      
        
            byte b[] = {75, 76, 77, 78}; 
            bout.write(b); 
    
            bout.close(); 
            fout.close(); 
        }   
     }
    
    
    Using BufferedOutputStream data can be transfer to a file with a faster rate.

    ObjectOutputStream:

    Creates an ObjectOutputStream that writes to the specified OutputStream.
    FileOutputStream fout = new FileOutputStream("file.txt"); 
    ObjectOutputStream oots = new ObjectOutputStream(fout); 
    
    import java.io.*;  
    class Main {  
    public static void main(String args[])throws Exception{    
         FileOutputStream fout = new FileOutputStream("file.txt"); 
            ObjectOutputStream oots = new ObjectOutputStream(fout); 
            String c = "Sateesh"; 
            oots.writeObject(c); 
            
            oots.close(); 
        }   
     }
    

    InputStream Classes:

    For all inputstream classes 'InputStream' is a predefined super class.

    FileInputStream Class:

    Which is used to read the single character from the file.
    FileInputStream fis=new FileInputStream("abc.txt");
                
    In the above statement will open the file in reading mode.If the file is not available at given location an error will be generated.
    int a=fis.read(); -- Read Single character form the file in the form of uni code.
    System.out.println((char)a);
    import java.io.*;
    import java.util.*;
    public class fileinputstreamDemo {
        public static void main(String args[]) throws IOException
        {
            FileInputStream fis=new FileInputStream("abc.txt");
            //System.out.println((char)fis.read());  one character
            int i;
            while((i=fis.read())!=-1)
            {
                System.out.println((char)i);
            }
            
        }
    
    }
                

    DataInputStream Class:

    Which can be used to read a complete line or statement at a time from the file.
    FileInputStream fis=new FileInputStream("abc.txt");
    DataInputStream dis=new DataInputStream(fis);
    System.out.println(dis.readLine()); -- It is used to read a line at a time.
    
    import java.io.*;
    import java.util.*;
    public class DataInputStramDemo {
        public static void main(String args[]) throws IOException
        {
            FileInputStream fis=new FileInputStream("abc.txt");
            DataInputStream dis=new DataInputStream(fis);
            String  i;
            while((i=dis.readLine())!=null)
            {
                System.out.println(i);
            }
            
        }
    
    }                
                

    ByteArrayInputStream Class:

    which can be used to read the data in the form of array.
    FileInputStream fis=new FileInputStream("abc.txt");
    ByteArrayInputStream bis=new ByteArrayInputStream(fis);
    
    byte b[]=new byte[20];
    for(int i=0;i<20; i++)
    {
    b[i]=bis.read();
    System.out.println((char)b[i]);
    }
                
        
    import java.io.*;  
    class Main {  
    public static void main(String args[])throws Exception{    
         byte[] buf = { 35, 36, 37, 38 ,66,67,68};  
        
        ByteArrayInputStream byt = new ByteArrayInputStream(buf);  
        int k = 0;  
        while ((k = byt.read()) != -1) {  
          
          char ch = (char) k;  
          System.out.println( k+ "= " + ch);  
        }  
        }   
     }
    

    BufferedInputStream Class:

    Which can be used to read the file information in the form of multiple buffers(arrays)
    FileInputStream fis=new FileInputStream("abc.txt");
    BufferedInputStream bfis=new BufferedInputStream(fis);
    bfis.read();
    
    import java.io.*;  
    class Main {  
    public static void main(String args[])throws Exception{    
        FileInputStream fin=new FileInputStream("abc.txt");    
        BufferedInputStream bin=new BufferedInputStream(fin);    
        int i;    
        while((i=bin.read())!=-1){    
         System.out.print((char)i);    
        }    
        bin.close();    
        fin.close();    
        }   
     }    
    

    SequenceInputStream Class:

    Which is used to read the data from multiple files in a sequencial order.any number of fileinputstream classes attached to SequenceInputStream
    FileInputStream fis1=new FileInputStream("abc.txt");
    FileInputStream fis2=new FileInputStream("xyz.txt");
    SequenceInputStream sis=new SequenceInputStream(fis1,fis2);
    sis.read(); -- it will read the single character from the first file
    
    OR
    FileInputStream fis1=new FileInputStream("abc.txt");
    FileInputStream fis2=new FileInputStream("xyz.txt");
    SequenceInputStream sis=new SequenceInputStream(fis1,fis2);
    DataInputStream dis=new DataInputStream(sis);
    dis.readLine(); -- it will read the First Statement  from the first file
    
    import java.io.*;
    import java.util.*;
    public class SequenceInputStreamDemo {
        public static void main(String args[]) throws IOException
        {
            FileInputStream fis1=new FileInputStream("abc.txt");
            FileInputStream fis2=new FileInputStream("xyz.txt");
            SequenceInputStream sis=new SequenceInputStream(fis1,fis2);
            int  i;
            while((i=sis.read())!=-1)
            {
                System.out.println(i);
            }
            
        }
    
    }
    
    
    import java.io.*;
    import java.util.*;
    public class SequenceInputStreamDemo {
        public static void main(String args[]) throws IOException
        {
            FileInputStream fis1=new FileInputStream("abc.txt");
            FileInputStream fis2=new FileInputStream("xyz.txt");
            SequenceInputStream sis=new SequenceInputStream(fis1,fis2);
            DataInputStream dis=new DataInputStream(sis);
            String  i;
            while((i=dis.readLine())!=null)
            {
                System.out.println(i);
            }
            
        }
    
    }
    
    
    For 600 JAVA Programs Click Here