Wednesday, June 22, 2016

FileOutputStream and FileInputStream from IO Package

package colections;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileOutputDemo {

public static void main(String[] args) throws IOException {

FileOutputStream fos = null;
     FileInputStream fis = null;
     byte[] b = {25,47,30,25,24,28};
     int i=0;
     try{
        // create new file output stream - this one used to write stream of raw bytes such as image data to a file
        fos=new FileOutputStream("R:\\Bore\\Eclipse Workspace\\Testing\\src\\colections\\test.txt");
       
        // writes bytes to the output stream
        fos.write(b);
        // flushes the content to the underlying stream
        fos.flush();
        // create new file input stream -used to read stream of raw bytes from a file *-1 means reached end of the file*
        fis = new FileInputStream("R:\\Bore\\Eclipse Workspace\\Testing\\src\\colections\\test.txt");
        //skip(long n) : n defines no of bytes to be skipped in stream
        fis.skip(3);
        System.out.println(fis.getClass());
        while((i=fis.read())!=-1){
        int x = i;
        System.out.println(x);

 }
     }catch(Exception ex){
        ex.printStackTrace();
     }finally{
        if(fos!=null)
        //used to close system resources
           fos.close();
        if(fis!=null)
           fis.close();
     }
  }
}


No comments:

Post a Comment