Alex M
Alex M

Reputation: 59

Java code could not load static file from resources

Java code could not load static file from resources folder

/etc/services-available/java/testFile.txt (No such file or directory)

My code:

InputStream testFileContent = this.getClass().getResourceAsStream(File.separator+"testFile.txt");

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(testFileContent));

Upvotes: 0

Views: 591

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44970

Resources are loaded from within classpath, not filesystem. If you have /src/main/resources/testFile.txt it should be loaded with getResourceAsStream("/testFile.txt").

To load a regular file use the FileInputStream or the Files utility class:

Path path = Paths.get("/", "etc", "services-available", "java", "testFile.txt");
try (BufferedReader br = Files.newBufferedReader(path)) {
  ...
}

Upvotes: 1

Related Questions