?
Solved

FileNotFound exception in java

Posted on 2007-10-15
5
Medium Priority
?
655 Views
Last Modified: 2008-01-09
Why am I getting compiler error for the following code?

 public static void fun( File plainFile){
          
    FileInputStream fis = new FileInputStream(plainFile); ==> FileNotFoundException, why?
    byte b;
    int bi;
    char bc;
    byte bb[] = new byte[(int)plainFile.length()];
    int count=0;
             
    while (fis.available() != 0) {
        b = (byte)  fis.read();
        bi = (int) b;
        bc = (char) b;
             
        bb[count] = b;
         count++;
        };

       fis.close();

 }


0
Comment
Question by:4eyesgirl
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20083181
the file represented by that variable does not exist on the file system
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 20083190
u need to handle the exception or declare that it can be thrown

\eg.

 public static void fun( File plainFile) throws FileNotFoundException {
          ...
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20083197
sorry did not read properly, you need to handle the exception that can be thrown by that line, either cath it with a try catch block or add it to the signature like this:

public static void fun( File plainFile) throws FileNotFoundException
0
 
LVL 92

Expert Comment

by:objects
ID: 20083199
or to handle it in the method:

try
{
   FileInputStream fis = new FileInputStream(plainFile);
    byte b;
    int bi;
    char bc;
    byte bb[] = new byte[(int)plainFile.length()];
    int count=0;
             
    while (fis.available() != 0) {
        b = (byte)  fis.read();
        bi = (int) b;
        bc = (char) b;
             
        bb[count] = b;
         count++;
        };

       fis.close();
}
catch (FileNotFoundException ex)
{    ex.printStackTrace();
}
0
 
LVL 92

Expert Comment

by:objects
ID: 20083204
when a method being calls is declared to throw a (non-runtime) exception you need to define how it is to be hanlded. Either by using a catch block to catch and handle it.
Or declaring that your method throws that exception resulting in the caller of your method being required to handle it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question