-2

I am using JAVA 8, Servlet 3.1 and springMVC without the web.xml. I have placed a info.json file in src\main\resources\data. There is a util class from which i am trying to read the file. No matter what path i give am unable to read it. i get below exception :

java.nio.file.NoSuchFileException: WEB-INF\classes\data\info.json at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)

I am trying to read the file as below :

public static <T> T unMarshal(String json, Class<T> clazz) throws IOException, URISyntaxException {

        byte[] jsonData = Files.readAllBytes(Paths.get("WEB-INF\\classes\\data\\info.json"));
        return  objectMapper.readValue(jsonData, clazz);
    }

also tried paths like

C:\\domain\\src\\main\\resources\\data\\info.json src\\main\\resources\\data\\info.json data\\info.json

I have tried different paths also tried fully qualified path still unable to read it. Also tried File path to resource in our war/WEB-INF folder? but unable to get hold of the context instance. Kindly suggest.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Raghuveer
  • 2,859
  • 7
  • 34
  • 66
  • 1
    Your resource is an entry of your jar or war file. It's not a file on the file system. WEB-INF/classes is in the classpath of your webapp. Use YourClass.class.getResourceAsStream() to load the bytes of this resource. Not file IO. – JB Nizet Mar 03 '18 at 07:57
  • `YourClass.getResourceAsStream()` does not exists for my class. – Raghuveer Mar 03 '18 at 07:58
  • 1
    Sorry. YourClass.class.getResourceAsStream(). – JB Nizet Mar 03 '18 at 07:58
  • you can refer this question [Link](https://stackoverflow.com/questions/11210465/spring-mvc-get-file-under-web-inf-without-a-request) – Ajit Kamble Mar 03 '18 at 08:06

1 Answers1

0

try to get the path of the current directory and file with:

String filePath = new File("").getAbsolutePath();
System.out.println (filePath);

BufferedReader reader = new BufferedReader(new FileReader(filePath + "/src/DBTextFiles/Administrator.txt"));