Your class is in package projectname, so the code is looking for the resource file projectname/config.properties, but the file is not there.
To look for resource file config.properties, use:
getClass().getResource("/config.properties");
as documented in the javadoc of getResource().
And remember, the resource may not be a file on the file system, so don't use e.g. File to access it. To read the content of the resource, use getResourceAsStream() instead.
Just move the file directly to the project folder which calls Java (and something under the blue-blurred stripe you made :P).
If that doesn't help, then move the test123.txt file to FirstJavaProgram directory.
You can also change filename to one of these:
src/test123.txtFirstJavaProgram/src/test123.txt
I am not sure which one will be fine in your case.
Use the full path of the file instead.
Right click on your file in your project, select "Copy Path", and paste that in to the path of your file.
EDIT: You could also use a relative path for your file. If your file is located in resources/, then you could use the form ./resources/yourfile.txt.
├── resources
│ └── test123.txt
└── src
├── MainClass.java
Try the next:
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.csv");
If the above doesn't work, various projects have been added the following class: ClassLoaderUtil1 (code here).2
Here are some examples of how that class is used:
src\main\java\com\company\test\YourCallingClass.java
src\main\java\com\opensymphony\xwork2\util\ClassLoaderUtil.java
src\main\resources\test.csv
// java.net.URL
URL url = ClassLoaderUtil.getResource("test.csv", YourCallingClass.class);
Path path = Paths.get(url.toURI());
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
// java.io.InputStream
InputStream inputStream = ClassLoaderUtil.getResourceAsStream("test.csv", YourCallingClass.class);
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader);
for (String line; (line = reader.readLine()) != null;) {
// Process line
}
Notes
- See it in The Wayback Machine.
- Also in GitHub.
Try:
InputStream is = MyTest.class.getResourceAsStream("/test.csv");
IIRC getResourceAsStream() by default is relative to the class's package.
As @Terran noted, don't forget to add the / at the starting of the filename