Opening a file after typing its name from command line?
OK it's been a real long while since I did any Java hacking, how can I do this again?
Here's some source I have from a book.
//ReadTextFile.java
// open the text file
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ReadTextFile
{
private Scanner input;
// enable user to open file
public void openFile()
{
try
{
input = new Scanner ( new File ("myfile.txt"));
} //end try
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file.");
System.exit(1);
} // end catch
} // end method openFile
// read records from file
public void readRecords()
{
int callID;
try
{
while (input.hasNext() )
{
callID = input.nextInt();
System.out.println(callID);
} // end while
} //end try
catch ( NoSuchElementException elementException )
{
System.err.println ("File improperly formed.");
input.close();
System.exit(1);
} // end catch
catch ( IllegalStateException stateException )
{
System.err.println ("Error reading from file." );
System.exit(1);
} // end catch
} // end method readRecords
// close file and terminate app
public void closeFile()
{
if (input != null )
input.close();// close file
} // end method closeFile
} // end class ReadTextFile
//ReadTextFileTest.java
public class ReadTextFileTest
{
public static void main( String args[] )
{
ReadTextFile application = new ReadTextFile();
application.openFile();
application.readRecords();
application.closeFile();
} // end main
} // end class
So at the command line I wanna do "java ReadTextFileTest openthisfile.txt" or whatever and have it open and read in some stuff from that file instead of hard coding it into the input = new Scanner ( new File ("myfile.txt")); line in the ReadTextFile.java
I'm sure it's pretty simple but like I said, very rusty.

