-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.

4
  • file can only be a existing file and not a directory. 10 secs of google and i found this docs.oracle.com/javase/tutorial/essential/io/dirs.html#listdir Commented Nov 24, 2016 at 8: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. Commented Nov 24, 2016 at 8: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. Commented Nov 24, 2016 at 8: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. Commented Nov 25, 2016 at 22:41

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.