简体   繁体   中英

How to get a String value of resources folder (Java)

I'm a beginner in programming and I have a class that requires a path to some folder in its constructor. For example:

 SomeClass class = new SomeClass(c:/folder);

I need to get a String value of resources folder (only path to folder without specific file names). A method in that class will add a filename to path from constructor and do some operations with a specific file.

I'm trying to do this (bad code!), but just have npe :

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("src/main/resources").getFile());
String path = file.getAbsolutePath();

So, the question is: how to get String value of resources folder?

What solved my problem:

File file = new File("src/main/resources");
String path = file.getAbsolutePath();

So, I got String path to resources folder.

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