>

Monday, April 14, 2014

FileInputStream Demo in Java


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 *
 * @author MALAR-KSP
 */
public class FileInputStreamDemo {
        public static void readFileContents(String location,String fileName)
    {
        if(location!=null&&fileName!=null)
        {
            FileInputStream fis= null;
            try {
                String file = location+File.separator+fileName;
                fis = new FileInputStream(file);
                BufferedInputStream bis= new BufferedInputStream(fis);
                int readContent = 0;
                while((readContent=bis.read())!=-1)
                {
                    System.out.print((char)readContent);
                }
                System.out.println("\nRead Completed....");
            } catch (FileNotFoundException ex) {
                System.out.println(""+ex.toString());
            } catch (IOException ex) {
                System.out.println(""+ex.toString());
            } finally {
                try {
                    fis.close();
                } catch (IOException ex) {
                    System.out.println(""+ex.toString());
                }
            }
        }
    }
    public static void main(String[] args) {
       
        String location="K:";
        String fileName = "TestFile.doc";
         
        readFileContents(location, fileName);
    }
}

No comments:

Post a Comment

Please post your comments. Your comments make us to write more programs for you.