Search code examples
javafileinputstreamfilenotfoundexceptionfileinputstream

FileInputStream throws FileNotFoundException


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.


Solution

  • 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.