0

I have the following code for reading a excel sheet in Java using the Apache POI. Although the file exists, why does it give me a FileNotFound Exception?

import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.io.FileInputStream;
import java.io.File;


    public class ReadFromExcel {

        public static void main(String[] args) {

            FileInputStream file = new FileInputStream(new File("C:\\Personal\\test.xlsx"));

        }
    }

I just copied and pasted the File location from windows explorer so I know that the file exists for sure. Then Why can't Java find it?

Used same path with the "File" class instead of "FileInputStream" and it works fine. What is special about paths in the class FileInputStream?

Amit
  • 41
  • 6
  • Try to put your file in another directory and then access it... – Ashish Ratan Jan 26 '14 at 08:53
  • 3
    Also double check that the file isn't open by any other process (e.g. Excel itself). That exception is thrown in the following cases- if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading. – robnick Jan 26 '14 at 08:54
  • may be it doesn't have access to read that file or try replacing `//` with `System.getProperty("path.saperator");` – jmj Jan 26 '14 at 08:58
  • @cox This path isn't relative, and absolute paths work in Java. FileInputStream doesn't have any methods like 'fromURL'. What exactly are you talking about? – user207421 Jan 26 '14 at 09:00
  • Used same path with the "File" class instead of FileInputStream and it works fine. What is special about paths in the class FileInputStream? – Amit Jan 26 '14 at 09:05
  • "Used same path with the "File" class instead of FileInputStream and it works fine". Q: What do you mean? What exactly did you do that "worked"? – FoggyDay Jan 26 '14 at 09:16
  • 1
    In the future, please COPY AND PASTE THE EXACT ERROR MESSAGE!!!!! Your post made it sound like a runtime error (FileInputStream was unable to open "C:\Personal\test.xlsx"). Your solution implies it was actually A COMPILE ERROR! Two completely different things; two completely different solutions. Please - in the future, always post the EXACT error. – FoggyDay Jan 26 '14 at 19:14
  • @cox So suppose you delete from here then? It being irrelevant? – user207421 Jan 27 '14 at 21:25

3 Answers3

0

Maybe the first two '\' only represent one '\', so you can use file path as "C:\\Personal\test.xlsx"

0

Suggestion: call File.canRead() to see if you have permissions to open the file.

Java new File() says FileNotFoundException but file exists

There are three cases where a FileNotFoundException may be thrown.

  1. The named file does not exist.
  2. The named file is actually a directory.
  3. The named file cannot be opened for reading for some reason.
Community
  • 1
  • 1
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • If I use File class instead of FileInputStream then the file.canRead() gives true. Since it won't work with FileInputStream I am not able to test it. Thanks for the feedback. – Amit Jan 26 '14 at 09:16
  • 1
    One other hint: for Windows, you can also use forward slashes: `new File("C:/Personal/test.xlsx");` – FoggyDay Jan 26 '14 at 09:22
0

Try this code:

import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileNotFoundException;


public class ReadFromExcel {

    public static void main(String[] args) throws FileNotFoundException {
        File f=new File("C:"+File.separator+"Personal"+File.separator+"test.xlsx");
        FileInputStream file=null;
        if(f.exists()) {
            file = new FileInputStream(f);
            //rest of code
        } else{
            System.out.println("The file does not exist!Please enter correct filename!");
        }
    }
}

I have 3 things to point out:

  1. Firstly you have not added a try/catch block.My IDE simply does not let it compile!
  2. Using File.separator is the more recommended way instead of using "\" or "/" if you are using file paths as they depend on OS.They make your code more portable.
  3. Checking that the file whether exists or not using f.exists() would let you know if actually the file you are trying to pass as parameter to FileInputStream exists. Sure that would help!!
rahulserver
  • 10,411
  • 24
  • 90
  • 164