Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundException in src/main/resources

Tags:

i placed a file in my maven project under src/main/resources the files name is simply temp.txt.

When i try to open the file:

BufferedReader br = new BufferedReader(new FileReader(new File("./temp.txt"))); 

I get an error:

Exception in thread "main" java.io.FileNotFoundException: \temp.txt 

all files under src/main/resources are placed in the root folder of the classpath under maven. So why cant the program find the file here?

like image 981
Mulgard Avatar asked May 08 '14 16:05

Mulgard


People also ask

How to avoid the java FileNotFoundException when loading resources?

Resources getResourceAsStream() looks at the classpath for the given resource. The leading slash on the input to getResourceAsStream() tells the loader to read from the base of the classpath. The contents of our JAR file are on the classpath, so this method works.

How do I get src main resources?

File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file. getAbsolutePath(); System. out.

How do I stop FileNotFoundException in Java?

How to avoid FileNotFoundException in java? Please ensure file exists when you try to read from file from path exists (using FileInputStream) to avoid FileNotFoundException. Please ensure file exists when we try to acces file from invalid path using RandomAccessFile to avoid FileNotFoundException.

What is the meaning of FileNotFoundException?

java.io.FileNotFoundException. Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.


1 Answers

If you're going to package the file in the class path, then read it as such.. from the class path.

Maven Structure

src    main        resources                file.txt 

After it builds, the file gets placed in the root of the class path. So use

InputStream is = getClass().getResourceAsStream("/file.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

The / in front of file.txt will bring you to the root, from whatever package the class is in.


UPDATE

Test example

package com.underdogdevs.stackoverflow;  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;  public class TestResourceFile {      public static void main(String[] args) throws IOException {         InputStream is = TestResourceFile.class.getResourceAsStream("/test.txt");         BufferedReader reader = new BufferedReader(new InputStreamReader(is));         String line;         while ((line = reader.readLine()) != null) {             System.out.println(line);         }     } } 

enter image description here

enter image description here

like image 154
Paul Samsotha Avatar answered Oct 16 '22 13:10

Paul Samsotha