Get FileNotFoundException when initializing FileInputStream with file

I am trying to initialize a FileInputStream object using a File object. I get a FileNotFound error in a line

fis = new FileInputStream(file); 

This is strange since I opened this file using the same method in order to execute the regex multiple 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 denied)
in java.io.FileInputStream.open (native method)
in java.io.FileInputStream. (Unknown source)
in java.io.FileInputStream. (Unknown source) on the controller .ScanEditRegions.fileToBIS (ScanEditRegions.java:52) on the controller .ScanEditRegions.tidyHTML (ScanEditRegions.java:38) on the controller .ScanEditRegions.process (ScanEditRegions. ScanEditRegions.java:148) on the .Manager.main controller (Manager.java:10)

+4
source share
4 answers

Judging by the stacktrace that you inserted into your post, I would assume that you do not have permission to read the file.

The File class allows you to perform useful file checks, some of which are:

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

For example, you can check: exists () && isFile () && canRead () and print the best error message, depending on the reason you cannot read the file.

+9
source

This is due to the file permissions settings in the OS. You started the Java process as a user who does not have access to a specific directory.

+3
source

You can make sure that (in the order of the probable hood):

  • The file exists.
  • The file is not a directory.
  • You or the Java process have permissions to open the file.
  • Another process does not have a file lock (most likely, since you are likely to get a standard IOException instead of a FileNotFoundException)
+3
source

I think that you are executing the instruction from eclipse or any Java IDE, and the target file is also present in the IDE workspace. You get an error because Eclipse cannot read the target file in the same workspace. You can run your code from the command line. It should never be.

-1
source

Source: https://habr.com/ru/post/1286268/


All Articles