Intellij Idea problem with text file impossible to read

落花浮王杯 提交于 2019-12-22 04:34:06

问题


I have a problem using Intellij Idea.

I am absolutely unable to load text file as InputStream - it doesnt matter where do I put the file (main/java, main/resources...) it just can't find the file - in Eclipse everything works just fine.

I tried setings->compiler->resource patterns and added ?*.txt but that doesn't seem to work either.

Any help is appreciated.


回答1:


If you load it as a File, make sure that Working Directory is properly set in IDEA Run/Debug Configuration, since it's the default directory where Java will look for a file when you try to access it like new File("file.txt"). Working directory should be set to the directory of your project containing .txt files.

If you load files as a classpath resource, then they should reside somewhere under Source root and will be copied to the classpath according to Settings | Compiler | Resource Patterns.

If you can't get it working, upload your project somewhere including IDEA project files so that we can point to your mistake.




回答2:


Look at the image, notice that the txt files are in the project root, and not the source folders (in blue).




回答3:


If you open the Project Structure dialog, and click on Modules and select your module - are the correct folders marked as Source Folders on the sources tab?

Link for how to get to Project Structure dialog

Also, if you print out the absolute path of that file you are trying to read, is that anywhere near where you expect it to be?




回答4:


An easy way to figure out the same would be to try creating a file in the same fashion and see where it gets created in your project. You can put your input file at the same location and it should work just fine (if it doesn't, you should check your resource pattern which might be causing the file to be not copied over in the build output).

This method actually gives you the working directory of your intellij settings which is pointed out in the accepted answer. Just sharing as I had similar trouble and I figured out this way. :)



来源:https://stackoverflow.com/questions/7106892/intellij-idea-problem-with-text-file-impossible-to-read

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