java.io.FileNotFoundException when creating FileInputStream

前端 未结 2 1195
北恋
北恋 2021-01-26 11:09

Getting an error when trying to open a FileInputStream to load Map from file with .ser extension.

Constructor where I create new File and invoke method that loa

相关标签:
2条回答
  • 2021-01-26 11:15
    • The path to the file you have given might be wrong for IDE it can take relative path but from the command line, it will take the absolute path.
    0 讨论(0)
  • 2021-01-26 11:18

    You provide a relative path for the file. That means program will look for the file relative to the working directory.

    Depending on how you run the program it will be the directory you run it from (if run from Shell/Cmd) or whatever is configured in the project settings (if run from the IDE). For the latter, it depends on the IDE but usually it's the project root directory.

    More info on working directory: https://en.wikipedia.org/wiki/Working_directory
    More info on relative path: https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths

    Regarding creation of the file, it would create non-existing file if you were to write to it. When you read it, it expects it to exist. That means you have to create empty file (if one does not exist) before reading or simply treat exception as empty content.

    0 讨论(0)
提交回复
热议问题