简体   繁体   中英

When using IntelliJ (cmd line template) , where can I locate the root directory for resource files?

While using IntelliJ by Jet-brains, I currently have to write the entire path for resource type files, like .txt and .csv, each time my program prompts for a file name. Is there a default root directory inside projects that are created using the default 'cmd-line template' in which I can place a .csv/.txt file and eliminate the need for writing the entire path when my program prompts for a file name?

Create resources directory in root of your project. If you use maven the resources directory will be automatically created as src/main/resources . Usually this directory is marked as resources root:

在此处输入图片说明

Now you can get file like:

URI uri = ClassLoader.getSystemResource("test.csv").toURI();
File file = new File(uri);

You must place resource files in the root directory of your project, but this cannot be done until the root directory is marked as as the resource root directory. This is done Inside IntelliJ by:

  1. Right clicking on the root directory
  2. Arrow over using the Mark Directory option
  3. Click on Resource Root, (NOT test Resource Root) Place any resource files you wish to use in the very root of your project, and you should not have to enter entire paths into your program to open files during run-time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM