• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error reading a file

 
Alex Mun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't know how to read a file and display it in the screen to check if the reading is correct.

The code is the following:

import java.io.*;

public class LFichero
{

void leerfichero()
{
File inputFile = new File("BAP.R4XRQZ.PPRO");
FileReader f = new FileReader(inputFile);

try
{
int c;

while ((c = f.read()) != -1)
{
System.out.println(c);
}
}
catch (IOException e)
{
System.out.println("No se puede abrir el fichero");
}

f.close();
}

When I compile the program I display the following errors:

LFichero.java:10: unreported exception java.io.FileNotFoundException; must be ca
ught or declared to be thrown
FileReader f = new FileReader(inputFile);
^
LFichero.java:27: unreported exception java.io.IOException; must be caught or de
clared to be thrown
f.close();
^
2 errors

The second error I think I can solve it but the first I don't understand it because I have a catch(IOException e).

How can I solve it?

Thanks.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line:

is not inside a try block. You can't catch an exception unless the line that throws it is inside a try.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!