try-catch block reports to have an Error that I don't know how to fix
// This program opens a text file and reads the contents of the text
// file, name of which is specified as a command-line argument
Code java:
import java.io.*;
public class ReadFile
{
public static void main(String[] args) throws IOException
{
int i;
FileInputStream theFile;
try
{
theFile = new FileInputStream(args[0]);
}
catch(FileNotFoundException exc)
{
System.out.println("The file you requested was not found, please doublecheck the name");
}
//catches the ArrayIndexOutOfBounds for the commandline arguments when a user
//forgets to enter the name of the file as the command line arguments
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Abide by the syntax\n"+
"Usage: java ReadFile fileName");
}
// read bytes (the file ) until EOF is reached
do
{
i = theFile.read(); // reads from the file
if(i!= -1)
System.out.print((char)i);
} while(i != -1);
theFile.close();
} // ends main
} // ends class ReadFile
Re: try-catch block reports to have an Error that I don't know how to fix
Quote:
This program opens a text file and reads the contents of the text file, name of which is specified as a command-line argument
Ok then what happens?