Java FileNotFoundException even though file exists

前端 未结 2 986
梦毁少年i
梦毁少年i 2021-01-24 04:39

I am trying to read a file located at \"C:\\Users\\Siddharth\\Documents\\aarti\\yeh_vidhi_mangal.txt\". Following code indicates that file exists

String filename         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-24 05:20

    According to the documentation for FileInputStream, "If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown." (emphasis mine) The file may be locked or in use by another application.

    What does file.canRead() return?


    Now that you've updated your question with more data, I can see that you are misinterpreting the error message. The error is that you are calling a method which throws a certain type of exception and you are not properly reporting or handling the exception. You can either add a try / catch for FileNotFoundException or add a throws to your method declaration which states that FileNotFoundException can be thrown.

提交回复
热议问题