File handling: Reading a file by inputting file name via CLI

Posted: February 28, 2012 in Java Programs

 

import java.io.*;
class showfile
{
public static void main(String arg[]) throws IOException
{
int i;
InputStream fis=new FileInputStream(arg[0]);

while(true)
{
i=fis.read();
if(i==-1)
break;
else
System.out.print((char)i);
}
fis.close();
}
}

NOTE: COMPILE THE PROGRAM FIRST FOLLOWED BY THE COMMAND:

java <classname> <filename.filetype>

               eg: java showfile mytext.txt

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s