Friday, 3 February 2017

FILEINPUTSTREAM AND FILEOUTPUTSTREAM IN JAVA

FileInputStream(FIS) class must be used to read data from a file. It is a two step process –
  • ·        Create FileInputStream class object by using its available constructors.
  • ·     Then read() must be called in loop, because it returns only one byte at a time. Hence it must be called in loop for each byte available in the file till it returns “-1” if control reaches the end of the file.


Constructors to create FIS objects
  • ·        FileInputStream(String name) throws FileNotFoundException
  • ·        FileInputStream(File file) throws FileNotFoundException
  • ·        FileInputStream(FileDescriptor fd)


Note – FIS and FOS class constructor does not throw FileNotFoundException. If file does not exist it creates empty file with the given name, then it writes the given data to that file.


FileOutputStream(FOS) class is used to write data to a file. It is also a two step process –
  • ·        Create FOS object by using its available constructors.
  • ·        Then call write() method to write either single or multiple bytes.


Constructors to create FOS object
  • ·        public FileOutputStream(String name) throws FileNotFoundException
  • ·        public FileOutputStream(File f) throws FileNotFoundException
  • · public FileOutputStream(String name,boolean append) throws FileNotFoundException
  • · public FileOutputStream(File file,boolean append)  throws FileNotFoundException


Example –
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class File1 {
          public static void main(String[] args) throws IOException {
                   //create stream object connecting to abc.txt
                   FileInputStream fis = new FileInputStream("src/abc.txt");
                   //reading all bytestream of the file till control reach end of file
                   int i = fis.read(); //reads only one character
                   System.out.println(i); //printing the ascii value of a character
                   // converting it to character
                   System.out.println((char)i);
                   System.out.println();
                   // reading all character
                   while((i=fis.read())!=-1)
                   {
                             System.out.print((char)i);
                   }
                   fis.close();
System.out.println();
                   FileOutputStream fos = new FileOutputStream("src/xyz.txt");
                   fos.write('a');
                   fos.write('b');
                   fos.write(100);
                   System.out.println("Data written into file xyz.txt");
                   fos.close();          
          }
}

Output –
105
i

 love coding
i love java
i am a hacker

Data written into file xyz.txt


Limitations of FIS and FOS -
FIS and FOS allows us to read and write data only in the format of bytes. It is not possible to read or write data in the format of primitive data or objects. So to overcome with this problem we should use FilterInputStream and FilterOutputStream classes in connection with FIS and FOS classes.

No comments:

Post a Comment