A
Andy K
I get this error message when I run the code below. Your help would be
greatly appreciated.
Error
============================================================
File to be read ? java.io.IOException: The handle is invalid
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:185)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:227)
at java.io.BufferedInputStream.read(Compiled Code)
at java.io.FilterInputStream.read(FilterInputStream.java:97)
at java.io.InputStreamReader.fill(Compiled Code)
at java.io.InputStreamReader.read(InputStreamReader.java:239)
at java.io.BufferedReader.fill(Compiled Code)
at java.io.BufferedReader.readLine(Compiled Code)
at java.io.BufferedReader.readLine(BufferedReader.java:329)
at PremiershipLeague.main(Compiled Code)
Exception in thread "main"
=================================================
here is the code.
import java.io.*; // this is needed to input and output data
import java.util.*; // this is needed to make use of the StringTokenizer
public class PremiershipLeague
{
static BufferedReader fileInput; // this is needed to input text file
characters
static BufferedReader keyboard = new BufferedReader(new
InputStreamReader(System.in));
// this is needed to input from the keyboard
public static void main(String Args[]) throws IOException
{
String textLine,fileName;
boolean moreToRead = true, fileNotFound;
//open the file
do
{
fileNotFound = false;
System.out.print("File to be read ? "); System.out.flush();
fileName = keyboard.readLine();
try
{
fileInput = new BufferedReader( new FileReader(fileName));
}
catch(FileNotFoundException e)// if file is not found
{
System.out.println("File not found " + e.toString());
fileNotFound = true;
}
} while(fileNotFound);
// input from the file
textLine = fileInput.readLine(); // Unicode format strings
moreToRead = (textLine != null);
while(moreToRead)
{
String teamName;
int points;
Premiership a[] = new Premiership[20]; // create array of object type
Premiership with 20 elements
int i = 0;
StringTokenizer tokens;
// this is needed to be able to split a string object into separate
components
tokens = new StringTokenizer(textLine); // to split the read string
into tokens
teamName = tokens.nextToken();
// the team name component extracted from the string
points = Integer.parseInt(tokens.nextToken());
// the points component extracted from the string
a = new Premiership(teamName, points); /* insert object into the
array using Premiership constructor
from class Premiership*/
i++;
System.out.println (a.getTeamName() + " " + a.getPoints());
textLine = fileInput.readLine(); // Unicode format string
moreToRead = (textLine != null);
} // end of while (moreToRead)
fileInput.close(); // close file
System.out.println( "File read and closed" );
} // end of main
} // end of class PremiershipLeague
greatly appreciated.
Error
============================================================
File to be read ? java.io.IOException: The handle is invalid
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:185)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:227)
at java.io.BufferedInputStream.read(Compiled Code)
at java.io.FilterInputStream.read(FilterInputStream.java:97)
at java.io.InputStreamReader.fill(Compiled Code)
at java.io.InputStreamReader.read(InputStreamReader.java:239)
at java.io.BufferedReader.fill(Compiled Code)
at java.io.BufferedReader.readLine(Compiled Code)
at java.io.BufferedReader.readLine(BufferedReader.java:329)
at PremiershipLeague.main(Compiled Code)
Exception in thread "main"
=================================================
here is the code.
import java.io.*; // this is needed to input and output data
import java.util.*; // this is needed to make use of the StringTokenizer
public class PremiershipLeague
{
static BufferedReader fileInput; // this is needed to input text file
characters
static BufferedReader keyboard = new BufferedReader(new
InputStreamReader(System.in));
// this is needed to input from the keyboard
public static void main(String Args[]) throws IOException
{
String textLine,fileName;
boolean moreToRead = true, fileNotFound;
//open the file
do
{
fileNotFound = false;
System.out.print("File to be read ? "); System.out.flush();
fileName = keyboard.readLine();
try
{
fileInput = new BufferedReader( new FileReader(fileName));
}
catch(FileNotFoundException e)// if file is not found
{
System.out.println("File not found " + e.toString());
fileNotFound = true;
}
} while(fileNotFound);
// input from the file
textLine = fileInput.readLine(); // Unicode format strings
moreToRead = (textLine != null);
while(moreToRead)
{
String teamName;
int points;
Premiership a[] = new Premiership[20]; // create array of object type
Premiership with 20 elements
int i = 0;
StringTokenizer tokens;
// this is needed to be able to split a string object into separate
components
tokens = new StringTokenizer(textLine); // to split the read string
into tokens
teamName = tokens.nextToken();
// the team name component extracted from the string
points = Integer.parseInt(tokens.nextToken());
// the points component extracted from the string
a = new Premiership(teamName, points); /* insert object into the
array using Premiership constructor
from class Premiership*/
i++;
System.out.println (a.getTeamName() + " " + a.getPoints());
textLine = fileInput.readLine(); // Unicode format string
moreToRead = (textLine != null);
} // end of while (moreToRead)
fileInput.close(); // close file
System.out.println( "File read and closed" );
} // end of main
} // end of class PremiershipLeague