2

Final edit: now the code looks like this

InputStream is = getClass().getResourceAsStream("/static/master-key.txt");
String masterKey = null;
Scanner scanner = new Scanner(is);
masterKey = scanner.nextLine();
System.out.println("the master key is " + masterKey);

Original post

I have a problem when reading txt file from resources folder.

This what project structure looks like:
projStructure

When I invoke the following code

System.out.println(getClass().getClassLoader().getResource("static/master-key.txt").getPath());
File mkFile = new File(getClass().getClassLoader().getResource("static/master-key.txt").getPath());

this what happens

/D:/Dropbox/Coding/Intellij%20IDEA/TishenkoKPO/target/classes/static/master-key.txt
java.io.FileNotFoundException: 
D:\Dropbox\Coding\Intellij%20IDEA\TishenkoKPO\target\classes\static\master-key.txt (System cannot find the specified path)

I've googled alot but have no clue of why this happens

Edit 1: Part of the code rebuilt as community suggested (working with file as a resource)

InputStream is = getClass().getResourceAsStream("static/master-key.txt");
String masterKey = null;
Scanner scanner = new Scanner(is);
masterKey = scanner.nextLine();
System.out.println("the master key is " + masterKey); //successfuly outputs the first line if exists 

Edit 2: resourcepath should begin with /

InputStream is = getClass().getResourceAsStream("/static/master-key.txt");
0

2 Answers 2

3
InputStream is = getClass().getResourceAsStream("static/master-key.txt");

This is searching for a resource relative to the package / directory of the class that is calling it. It is likely that resource can actually be found relative to the root of the application structure. To do that, add a leading /, like this.

InputStream is = getClass().getResourceAsStream("/static/master-key.txt");
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

getResource("resources\static\master-key.txt").getPath());

and if u targeting a path use '\' not '/'. And u can try

YourClass.class.getResourceAsStream("resources\static\master-key.txt");

or

YourClass.class.getResourceAsStream("static\master-key.txt");

4 Comments

That's weird. I've tried everything you've mentioned but nothing works. It throws Nullpointerexception as if there's no file by that filepath.
You have file in this path ? D:\Dropbox\Coding\Intellij%20IDEA\TishenkoKPO\target\classes\static\master-key.txt
Indeed, I have. Could it be that 'space' in the name of a folder causing the problem? Java interprets 'space' as '%20'
Edit: no, that's not the case. Have just tested that on a brand new project, works fine. Maybe the problem lays in the location of the class which method I invoke.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.