Powered by Blogger.

5 ).Write a program to read a file by sending the argument as a file name at runtime.

>> Thursday, April 28, 2011

Here we are using BufferedReader to read the name of the file.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


Read the file using FileInputStreamReader then send it to BufferedInputStreamReader to read all the data with InputStreamReader provided mehtods.



package javabynataraj.iopack;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileArgs {
    public static void main(String[] args)throws IOException {
        int ch;
        String fname;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter file name: ");
        fname = br.readLine();
        FileInputStream fis = new FileInputStream(fname);
        BufferedInputStream bis = new BufferedInputStream(fis);
        while((ch=bis.read())!=-1){
            System.out.print((char)ch);
        }
        fis.close();
        bis.close();

    }
}


Note:
You should have one serialized file or any file to enter the name at runtime .you should enter filename with extension like ' hello.dat '.

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.