Its been a while since I did Java and so am going through a book. As far as I can see the following code is correct however I am having trouble compiling it. I used to use Scanner
, but this book is taking this approach, and I would prefer to follow it and do the exercises as explained, can anyone see what is wrong here?
import java.io.*;
class ReadFile
{
public static void main( String[] args )
{
try
{
FileReader file = new FileReader("Sheffield.data");
}
catch (IOException e)
{
System.out.println("A read error has ocurred" );
}
BufferedReader buffer = new BufferedReader(file);
String line = "";
while ((line = buffer.readLine()) != null)
{
System.out.println(line);
}
buffer.close();
}
}
The error I am getting in Windows cmd is as follows:
Simple error I know, any help will be much appreciated.
FIXED!!
BufferedReader buffer = new BufferedReader(file);
String line = "";
while ((line = buffer.readLine()) != null)
{
System.out.println(line);
}
buffer.close();
the above should all be placed in the try
statement under the FileReader file = new FileReader("Sheffield.data");