Java.io.FileNotFoundException error message

I have a simple program that checks if my file exists.

I copied the file in my working folder (with my Java classes) and in c :.

when called, the new FileInputStream("1.xls")code generates an exception error!

Exception in thread "main" java.io.FileNotFoundException: 1.xls (Le fichier spécifié est introuvable)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at NewMain.main(NewMain.java:25)

when called, new FileInputStream("c:\\1.xls")it works!

Here is my code

public static void main(String[] args) throws FileNotFoundException {
  // TODO code application logic here
  InputStream input = new BufferedInputStream(new FileInputStream("c:\\1.xls"));
  System.out.print("The file exists");
}
+4
source share
1 answer

I copied the file under my working folder (with my Java classes)

"1.xls" . IDE ( ) , . , ( ) , , src

- , , - , ,

FileInputStream ( "c:\1.xls" ), !

, .


. , , Java, , , . . URL. , YourClass.class.getResourceAsStream("/path/to/file"), InputStream. , -

InputStream is = YourClass.class.getResourceAsStream("/path/to/file");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

, , , , , , , , . /, , ( ) src. , src/resources/file.txt, "/resources/file.txt"

+3

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


All Articles