JAVA PROGRAM TO SHOW CONTENT OF A GIVEN FILE NAME AS A COMMAND LINE ARGUMENT.

/*write a java program to take file name as a command - line argument  and dipslay the content of file , if it exist.*/PROGRAM CODE--


import java.io.*;

class ShowFile
{
public static void main(String ar[])
{

FileInputStream fin;
int i;

//first,confirm that file name is specified or not
if(ar.length!=1)
{
System.out.println("Usage: ShowFile filename");
return;
}

//to open the file
try
{
fin = new FileInputStream(ar[0]);
}catch(FileNotFoundException e)
{
System.out.println("Cannot Open File !!given file is not exist");
return;
}

//to get data from file, if file successfully opened
try
{

fin = new FileInputStream(ar[0]);

System.out.println("content of given file is :");
do
{
i=fin.read();
if(i!=-1)
System.out.print((char) i);
}while(i!=-1); //-1 represent the end of file
}catch(IOException ie)
{
System.out.println("Error Reading File");
}


//file closing
try
{

fin = new FileInputStream(ar[0]);
fin.close();
}catch(IOException ae)
{
System.out.println("Error Closing  File");
}

}

}


 THIS IS A DATA1.TXT FILE 

 OUTPUT OF ABOVE PROGRAM---


Comments