开发者

Intellij Idea problem with text file impossible to read

开发者 https://www.devze.com 2023-03-29 03:43 出处:网络
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 -

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 eith开发者_如何学Cer.

Any help is appreciated.


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.


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

Intellij Idea problem with text file impossible to read


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?


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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号