Search code examples
javafilereaderfilenotfoundexceptionfileinputstream

FileNotFoundException while reading a xlsx file


I'm trying to read a .xlsx file from the project folder using the following code, but it always throws FileNotFoundException. I have attached the project structure where the file is.

public static void main(String[] args)  {
    try {
        String excelFilePath = "‪DataModel.xlsx";
        File file = new File(excelFilePath);
        FileInputStream fis = new FileInputStream(file);         

    } catch (Exception ex) {
        System.out.print(ex);
    }
}

enter image description here


Solution

  • This is how I normally would approach it:

    File file = new File( javaApplication2.class.getResource( excelFilePath ).getPath() );