File's in java, (Byte-oriented)key notes

I/O Stream

  • Stream is medium or channel, it will allow the data in continuous flow from input devices to java program and from Java program to output devices
  • Java uses the concept of a stream to make I/O operation fast.
  • The java.io package contains all the classes required for input and output operations.
  • A stream can be defined as a sequence of data.
  • In Java IOStreams are divided into following ways: 
    • Byte oriented Streams. 
    • Character-Oriented Streams    

Byte-Oriented Streams:

  • These are Streams, which will allow the data in the form of bytes from input devices to Java  program and from java program to output devices. 
  • The length of the data in byte-oriented streams is 1 byte. 
  • There are two types of Byte-Oriented Streams 
    • InputStream
      • It is a byte-oriented Stream,it is used to read data from a source.
      • InputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes.
      • This are subclass( ByteArrayInputStream ,FilterInputStream,DataInputStream,ObjectInputStream, FileInputStream , BufferedInputStream )
    • OutputStream
      • It is a byte-oriented Stream,it is used for writing data to a destination.
      • OutputStream class is an abstract class. It is the superclass of all classes representing an output stream of bytes.
      • this are the subclass (ByteArrayOutputStream, FilterOutputStream, DataOutputStream, FileOutputStream, ObjectOutputStream , BufferedOutputStream)

Working of Java OutputStream and InputStream

Hierarchy of classes to deal with Input and Output streams

Standard Streams

  • All the programming languages provide support for standard I/O where the user's program can take input from a keyboard and then produce an output on the computer screen
  • In Java, 3 streams are created for us automatically. All these streams are attached with the console.

1) System.in: standard input stream

2) System.out: standard output stream

3) System.err: standard error stream

Example:

import java.io.IOException;
class Innerclass {
     public static void main(String[] args) throws IOException {
                int i=0;
                System.out.println("Simple Example for Standared Stream's"); 
                System.out.println("keep on accepting a input till Exit(for Exit enter e)");
                do{
                    i=System.in.read();
                    if(i=='e'){
                                System.err.print("im exit from the iteration");
                              }else{
                                System.out.print((char)i); 
                        } }while(i!='e');
             } 
 } 

Output:

Simple Example for Standared Stream's
keep on accepting a input till Exit(for Exit enter e)
5
5
f
f
hj
hj
ghj9.55
ghj9.55
e
im exit from the iteration

FileOutputStream

  • It is byte-oriented Stream, it can be used to transfer the data from Java program to a particular target file.  
  • If you have to write primitive values into a file, use FileOutputStream class.
  • We can write byte-oriented as well as character-oriented data through FileOutputStream class.
  • But, for character-oriented data, it is preferred to use FileWriter than FileOutputStream.
  • Constructors
    • public FileOutPutStream(String target_File) //It will override the existed data in the target file
    • public FileOutPutStream(String target_File,boolean b) //it will not override
  • FileOutputStream class methods
    • protected void finalize() -It is used to clean up the connection with the file output stream.
    • void write(int b) - It is used to write the specified byte to the file output stream.
    • FileChannel getChannel() - It is used to return the file channel object associated with the file output stream.
    • FileDescriptor getFD() - It is used to return the file descriptor associated with the stream.
    • void close() -It is used to closes the file output stream.

Example:(write byte to the target file)

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
           try{
             FileOutputStream fout=new FileOutputStream("H:\\testout.txt");  //It will override the existed data in the target file 
             fout.write(65); //write byte to the target file   
             fout.close();  //Close FileOutPutStream 
             System.out.println("successfully write a data into the target file");   
              } 
           catch(Exception e){
             System.out.println(e); 
           }	}   } 

Output:

Check Your file testout.txt in H directory it contains 

A

Example : (write String)

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
           try{
      FileOutputStream fout=new FileOutputStream("H:\\testout.txt",true);  //It will override the existed data in the target file 
             String s="welcome to Sookshmas"; // Declare the data
             byte b[]=s.getBytes(); // Converting string into byte array
             fout.write(b); //write byte to the target file   
             fout.close();  //Close FileOutPutStream 
             System.out.println("successfully write a data into the target file");   
              }catch(Exception e){
             System.out.println(e); 
           }	
} 
}

Output:

successfully write a data into the target file

check Your target file

Awelcome to Sookshmas

FileInputStream:  

  • It is a byte-oriented Stream,it can be used to transfer the data from a particular source file to Java Program. 
  • It is used for reading byte-oriented data (streams of raw bytes) such as image data, audio, video etc. You can also read character-stream data.
  • But, for reading streams of characters, it is recommended to use FileReader class.
  • Constructor
    • public FileInputStream(String file_name) throws FileNotFoundException //it throws an exception when the file is not existed.
  • FileInputStream class methods
    • int available() - It is used to return the estimated number of bytes that can be read from the input stream.
    • int read() - It is used to read the byte of data from the input stream.
    • long skip(long x) - It is used to skip over and discards x bytes of data from the input stream.
    • FileChannel getChannel() - It is used to return the unique FileChannel object associated with the file input stream.
    • FileDescriptor getFD() - It is used to return the FileDescriptor object.
    • protected void finalize() - It is used to ensure that the close method is call when there is no more reference to the file input stream.
    • void close() - It is used to closes the stream.

Example:(read single character)

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
           try{
         FileInputStream fin=new FileInputStream("H:\\testout.txt");  // create object and mention target file name   
             int i=fin.read();  // Read the data from File 
             System.out.println((char)i);   //convert it into charecter 
             fin.close();  //Close FileOutPutStream 
            } catch(Exception e){System.out.println(e); 
           }	
}  
}

Output:

A   // fetching the first charecter data only from the targeted file

Example:(read all characters)

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
           try{
FileInputStream fin=new FileInputStream("H:\\testout.txt");  // Create objeect and mention target file name  
             int size=fin.available(); // return the length of the data in file
             byte b[]=new byte[size]; //declaring byte array with the available size
             fin.read(b);  // Read the data from File 
             String data=new String(b); //convert it into String
             System.out.println(data);  
             fin.close();  //Close FileInPutStream 
            }catch(Exception e){
             System.out.println(e); 
           }
}
}

Output:

Awelcome to Sookshmas // fetching all the data from the targeted file

Continue reading with the other related topic:

File's in java, (Character-oriented)key notes

Difference between Byte and Character Oriented

Posted on by