1

I have a simple JavaEE web application project in Intellij and I need to read a file in src.main.resources folder. Follow is a screen shot of what my project looks like.

enter image description here

I need to return the context of data.txt file in the getData() function. Current method does nothing but return "Error" to the index.jsp and throw a NoSuchFileException. I tested using following paths but result is the same.

"data.txt"
"/data.txt"
"src/main/resources/data.txt"
"/src/main/resources/data.txt"

Any help would be greatly appreciated.

  • 1
    You have to either use the absolute path or the path relative to the Tomcat working directory (or change Tomcat working directory inside the server startup script). The better option could be to read it from the classpath instead. – CrazyCoder Apr 29 '17 at 18:49
  • Can you please show me how to do that relevantly to my environment? – Vihanga Liyanage Apr 30 '17 at 05:35
  • 1
    I'm sure you can find hundreds of answers how to do that, like [this one](http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java). – CrazyCoder Apr 30 '17 at 10:00

4 Answers4

1
System.out.println("get data");
    String newMessage = "error";
    String fileName = "abc.txt";
    try {
        newMessage = new String(Files.readAllBytes(Paths.get("C:\\Users\\kalingay\\IdeaProjects\\SpringMVCCrudApp\\src\\main\\resources\\" + fileName)), "UTF-8");
        System.out.println("out--->" + newMessage) ;
    } catch (Exception e) {
        System.out.println("Exception-->" + e);
    }
1
 System.out.println("get data");
    StringBuilder out = new StringBuilder();
    try {
        InputStream inputStream = HelloWorld.class.getResourceAsStream("kky.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
        System.out.println(out.toString());
        reader.close();
        System.out.println("out--->" + out);
    } catch (Exception e) {
        System.out.println("Exception-->" + e);
    }

Make Sure HelloWorld class and kky.txt in same folder ... :) :) This will help you :) :)

enter image description here

Using Relative Path .... :) :) try this one

enter image description here

1

First, create your java project structure correctly, place HelloWorld.java under src/main/java.

Put your resources under /src/main/resources.

Load the resources with:

.getClass().getClassLoader().getResource(fileName)
D00de
  • 880
  • 3
  • 7
  • 26
  • Complete solution I found is added as another answer. But your answer led me to it hence I'm marking this as the accepted answer. Thanks. – Vihanga Liyanage May 02 '17 at 10:10
0

With the help of @D00de I got together a complete solution as follows.

First thing to do is reorganize the project as shown in the image. Classes should go in src.main.java.* and resources should go in src.main.resources.*

Then to access the file, use following code.

URI uri = HelloWorld.class.getClassLoader().getResource("metadata/data.txt").toURI();
out = String.valueOf(Files.readAllLines(Paths.get(uri)));

enter image description here