java.io.FileNotFoundException although the file is present

允我心安 提交于 2019-11-29 17:41:10

You are clearly trying to access the file using a relative pathname. When you do this, the Java runtime system will attempt to find the file relative to the application's current directory. And the most likely reason it works in one scenario and not the other is the current directory is different in the two scenarios.

Try using an absolute pathname instead.

In cases of "the server can't find the file, but it's there!" problems, this will solve it (always has for me):

File file = new File("somefilename");
System.out.println(file.getAbsolutePath());    

You will quickly discover that the directory where the server is looking isn't what you thought it was. Usually because the home directory isn't what you think it is.

Spring MVC application probably run is some server - on contrary with main-function in class.

You root path would be different. You should put this file right next to WEB-INF folder. Should be located in a war folder. (If you have started from some good template)

The File class is just wrapper for manipulating abstract filenames. This code

File file=new File("ip-to-country.bin");
System.out.println(file.getCanonicalPath());

not requres the file exist and executes normally. Use File.exists() method to check existence of file. As you trying to access file with relative path, make sure it located in current directory of running application. Probably it is not same folder as project root.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!