How to add a text file to the project?

I have been trying to add a file to my project. I have placed the file parallel to the src folder.

I am calling the file via this piece of code

String next = "";
File mappingsFile = new File("/mapping.json");
try {
    Scanner scanner = new Scanner(mappingsFile);
    next = scanner.next();
} catch (FileNotFoundException e) {
    e.printStackTrace();
    next = e.getLocalizedMessage();
}


I keep getting the FileNotFoundException, specifically that the specified path cannot be found?

How can I accomplish this?

0
2 comments
Avatar
Permanently deleted user

Okay so I got it to work finally using the answer provided here. https://stackoverflow.com/questions/18717038/adding-resources-in-intellij-for-java-project

Message was edited by: Osama Rao

0

For anyone experiencing the same problem. I solved it by using the using the Classloader of the thirdparty class containing the code above and setting it on the current thread. Something like the following:

- resource
---- my.properties

 

 

Thread.currentThread().setContextClassLoader(org.your.clazz.ProblemCode.class.getClassLoader());
InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties");
0

Please sign in to leave a comment.