-2

I am trying to read the content of the folder of my Java EE Spring application, but it always return me null. The folder I want is under src/main/resources folder and it`s called context. I try doing it this way :

File file = new File("src/main/resources/context")

But it always return me null for file.

Mohamed Moanis
  • 477
  • 7
  • 18
user5783530
  • 221
  • 1
  • 4
  • 16
  • file can only be a existing file and not a directory. 10 secs of google and i found this https://docs.oracle.com/javase/tutorial/essential/io/dirs.html#listdir – XtremeBaumer Nov 24 '16 at 08:25
  • 1
    Directory should not be a problem for the `java.io.File` object. In fact, even if the file or folder does not exist, file` will get assigned a valid non-null object. – anacron Nov 24 '16 at 08:28
  • i would suggest, get current folder path using something like: System.getProperty("user.dir"); and then append your path of the folder to the path return by this getProperty method. – Ankush G Nov 24 '16 at 08:32
  • `src/main/resources` implies that you're using Maven to build your application, and that we're actually talking about a folder that is a project resources. To list its content, refer to the linked question -- you can't use the `File` approach. – Tunaki Nov 25 '16 at 22:41

1 Answers1

1

You can use one of the following:

    File file = new File("src/main/resources/context");
    String[] list = file.list(); // returns an array of all file names in the context folder.

OR

    File file = new File("src/main/resources/context");
    File[] listFiles = file.listFiles(); // returns an array of all "file objects" for all files in the context folder.

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31