Java FileNotFoundException even though file exists

前端 未结 2 984
梦毁少年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.

    0 讨论(0)
  • 2021-01-24 05:30

    Try using single slash rather than double slash like

    C:/User/Documents/your Filename.
    

    and use Backward Slash Because your File is going to inherit in the following Directories so write the path like this for ex:

    C:\User\Documents\Your Filename.
    
    0 讨论(0)
提交回复
热议问题