Get FileNotFoundException when initialising FileInputStream with File object
Asked Answered
M

4

6

I am trying to initialise a FileInputStream object using a File object. I am getting a FileNotFound error on the line

fis = new FileInputStream(file);

This is strange since I have opened this file through the same method to do regex many times.

My method is as follows:

private BufferedInputStream fileToBIS(File file){

    FileInputStream fis = null;
    BufferedInputStream bis =null; 
    try {
        fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
    return bis;
}

java.io.FileNotFoundException: C:\dev\server\tomcat6\webapps\sample-site (Access is denied)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.(Unknown Source)
    at java.io.FileInputStream.(Unknown Source)
    at controller.ScanEditRegions.fileToBIS(ScanEditRegions.java:52)
    at controller.ScanEditRegions.tidyHTML(ScanEditRegions.java:38)
    at controller.ScanEditRegions.process(ScanEditRegions.java:64)
    at controller.ScanEditRegions.visitAllDirsAndFiles(ScanEditRegions.java:148)
    at controller.Manager.main(Manager.java:10)

Mcilroy answered 16/6, 2009 at 5:57 Comment(1)
Is the file there, do you have permission issues?Gyral
S
10

Judging by the stacktrace you pasted in your post I'd guess that you do not have the rights to read the file.

The File class allows you to performs useful checks on a file, some of them:

boolean canExecute();
boolean canRead();
boolean canWrite();
boolean exists();
boolean isFile();
boolean isDirectory();

For example, you could check for: exists() && isFile() && canRead() and print a better error-message depending on the reason why you cant read the file.

Seaworthy answered 16/6, 2009 at 6:11 Comment(2)
The problem was a directory was being passed in (as well as many files)Mcilroy
same prob also i found and my error is 02-10 14:18:25.367: W/System.err(365): java.io.FileNotFoundException: /storage/emulated/0/bhanuдо_свидания.txt: open failed: ENOENT (No such file or directory) 02-10 14:18:25.367: W/System.err(365): at libcore.io.IoBridge.open(IoBridge.java:409) 02-10 14:18:25.367: W/System.err(365): at java.io.FileInputStream.<init>(FileInputStream.java:78)Frontlet
T
4

You might want to make sure that (in order of likely-hood):

  1. The file exists.
  2. The file is not a directory.
  3. You or the Java process have permissions to open the file.
  4. Another process doesn't have a lock on the file (likely, as you would probably receive a standard IOException instead of FileNotFoundException)
Temptation answered 16/6, 2009 at 6:4 Comment(1)
same prob also i found and my error is 02-10 14:18:25.367: W/System.err(365): java.io.FileNotFoundException: /storage/emulated/0/bhanuдо_свидания.txt: open failed: ENOENT (No such file or directory) 02-10 14:18:25.367: W/System.err(365): at libcore.io.IoBridge.open(IoBridge.java:409) 02-10 14:18:25.367: W/System.err(365): at java.io.FileInputStream.<init>(FileInputStream.java:78)Frontlet
D
3

This is has to do with file permissions settings in the OS. You've started the java process as a user who has no access rights to the specific directory.

Deplorable answered 16/6, 2009 at 5:59 Comment(0)
M
-1

I think you are executing the statement from eclipse or any java IDE and target file is also present in IDE workspace. You are getting the error as Eclipse cant read the target file in the same workspace. You can run your code from command prompt. It should not through any exception.

Meggie answered 24/3, 2012 at 15:34 Comment(1)
The original poster has already accepted an answer and explained the root cause of his problem - he was passing in a directory path rather than a file path. Please ensure you check what others have written before answering a historic question.Lorrianelorrie

© 2022 - 2024 — McMap. All rights reserved.