-1

This code throws FileNotFoundException.

Edit: As requested I have included the full StackTrace.

import java.io.FileInputStream;
import java.io.InputStream;

public class ReadFile{
    public static void main(String[] args){
        InputStream inputstream = new FileInputStream("C:\\file.txt");
    }
}

The file "file.txt" is at that location though. I would like to post a screenshot of this as requested, but I can't because I need at least 10 reputation points.

DrainZ
  • 23
  • 3
  • Have you double checked that the file exists and that you have the option to hide file extensions for known extensions disabled? Maybe the real name of the file is "file.txt.txt"... happened once to me. – maio290 Nov 16 '19 at 19:09
  • @ArvindKumarAvinash cmd responded with 'C:\' is not recognized as an internal or external command, operable program or batch file. – DrainZ Nov 16 '19 at 19:22
  • @maio290 What is this "hide file extensions for known extensions" you speak of? – DrainZ Nov 16 '19 at 19:25
  • @ArvindKumarAvinash well every line starts automatically with C:\Users\oskar>, and it can't be erased. So I can't write what you wnat me to write. But it doesn't really matter, because I know that the file is there, because I can see it in the file explorer. – DrainZ Nov 16 '19 at 19:29
  • @ArvindKumarAvinash I did as you instructed and I can see that the file is there. – DrainZ Nov 16 '19 at 19:34
  • @ArvindKumarAvinash Tried using that. It did not help. – DrainZ Nov 16 '19 at 19:59

3 Answers3

1

If you are 100% certain that the file exists and you're still getting a FileNotFoundException, than most likely your user or the user running Java has no permission to access this file (since I am using German Windows the dialog is in German, but as you can see "Benutzer" (which is Users) have a denied right to read and execute the file a.txt:

enter image description here

This however, results in a a FileNotFoundException with a localized error message returned :

Exception in thread "main" java.io.FileNotFoundException: C:\a.txt (Zugriff verweigert)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:131)
    at java.io.FileInputStream.<init>(FileInputStream.java:87)
    at Threadstuff.main(Threadstuff.java:50)

Zugriff verweigert means "access denied". If that isn't the problem either, I guess you should post your full StackTrace.

The other option I mentioned in my comment is an explorer option ("View" -> "Options") in the Folder and Search options -> View:

enter image description here

(roughly translates to "Hide extensions for known extensions")

If this is enabled, the filenames in the explorer are losing their extensions in the view. Meaning that they are shown as "file" instead of "file.txt" - which sometimes leads to the mistake of creating a "file.txt.txt" when renaming a file. And is/was also often used to trick users into thinking they were open a different kind of file (.pdf.exe) - mostly used by bad guys.

maio290
  • 5,118
  • 1
  • 14
  • 29
  • Hide extensions for known file types is enebled, but that doesn't cause any trouble as the file is listed as "file". – DrainZ Nov 16 '19 at 19:55
  • What do you mean Users have denied access to read? Zugriff means access and lesen means read, right? Then Users have access to read. – DrainZ Nov 16 '19 at 19:56
  • I guess it would be better if you were to supply the full stacktrace of the exception and a screenshot of the cmd printing your c folder (with the section which contains the file.txt) - yeah, right. But in Windows you can have deny ("Verweigern") and allow ("Zulassen") at the same time - deny trumps allow in this case. If it is about permissions, go to your C drive in the cmd and write icacls file.txt ... if it replies with "Access denied", well, then it's a permission problem. – maio290 Nov 16 '19 at 19:59
  • Well, my case is the same as yours. Users have access to read and execute, but lack access to modify and Write. Should this really cause a problem? Isn't reading access sufficeable? – DrainZ Nov 16 '19 at 20:03
  • cmd tells me this Users:(I)(RX) Admin: (I)(F) etc. What does it mean? – DrainZ Nov 16 '19 at 20:09
  • These are permissions. (I) was inherited as far as I recall (RX) is read and execute (F) is full-access see: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/icacls However, can you still provide us with a screenshot of the dir command showing the file.txt in your C drive and the FullStackTrace of the exception? – maio290 Nov 16 '19 at 20:13
  • Yes, I am on it – DrainZ Nov 16 '19 at 20:17
  • I have included the full StackTrace now. But I were unable to provide Screenshot. – DrainZ Nov 16 '19 at 20:28
0

Is this really the full Filepath? Better check that one. Also I'd recommend putting files that are to be read by your program e.g. Textfiles, Images and such into the classpath of your project so when you pack and export it the file paths are not obstructed by being on somebody else's PC where that file does not exist on that path and so on.

This answer suggest you transform the Path of the file to a java conform URL path.

Sepx3
  • 28
  • 10
  • Yes, it is the full path – DrainZ Nov 16 '19 at 19:10
  • [This](https://stackoverflow.com/a/52520400/10389963) answer suggests something that transforms the path you give the InputStream into a java conform path. It's a longshot but it might just be what you need. – Sepx3 Nov 16 '19 at 20:39
  • That worked for me. Thanks a lot! I still have no clue of what's wrong with my original code though. – DrainZ Nov 16 '19 at 21:10
  • My best guess is that it might just expect a different URL format than the actual filepath you copy from the explorer bar. But to be completely honest I am not experienced with InputStreams – Sepx3 Nov 16 '19 at 21:13
0

Try the below one.

InputStream inputstream = new FileInputStream("C:"+File.separator+"file.txt");

A better approach would be

File file = new File("C:"+File.separator+"file.txt");
if(file.exists()) {
   //Read the file
}
else {
   System.out.println("File does not exist);
}

To ensure whether file exists or not in windows, press windows button + r and then paste the file path you have mentioned and after that press enter key. If file is in that location, a notepad with file contents will be opened.

PythonLearner
  • 1,174
  • 2
  • 16
  • I still get FileNotFoundException – DrainZ Nov 16 '19 at 19:14
  • Updated the answer, check. – PythonLearner Nov 16 '19 at 19:18
  • What did you hope for? File.seperator is "\" on Windows Machines anyway. This really changes nothing here. And creating a File object to check if the file exists is not needed, because that's why FileInputStream throws the exception. So this would just result in the else case anyway. This approach isn't any better, it's just different. But if the OP only needs the InputStream, your's is more bloaty. – maio290 Nov 16 '19 at 19:22
  • Yes, as far as this context is concerned, I have suggested to write better way for different platforms. Besides I have also suggested OP to check the file run command in windows for text file. – PythonLearner Nov 16 '19 at 19:26