Reputation: 5979
My unit test project structure looks like this:
.
└── src
└── test
├── java
│ └── foo
│ └── MyTest.java
└── resources
├── file1
|-- ...
└── fileN
In MyTest.java, I would like to get a list of all files under src/test/resources/
:
File[] files = GET_ALL_RESOURCE_FILES();
how to do it in Java?
Upvotes: 0
Views: 1608
Reputation: 2074
Try this as your code:
File Selected_Folder = new File(System.getProperty("user.dir")+"/src/test/resources/");
File[] list_of_files = Selected_Folder.listFiles();
For viewing the files under that particular directory from where you have selected the files... you can do this...
for(int i=0; i<list_of_files.length; i++)
{
System.out.println(" File Number "+(i+1)+" = "+list_of_files[i]);
}
Upvotes: 4
Reputation: 1655
Try this File folder = new File(System.getProperty("user.dir")+"/src/test/resources/");
File[] files = folder.listFiles();
System.getProperty("user.dir")
will return path of your current working directory. In maven projects it returns path of your maven project folder which is parent folder of src folder.
Upvotes: 0
Reputation: 4604
My Approach--
File[] GET_ALL_RESOURCE_FILES(final File file)
{
java.util.List<File> ls = new ArrayList<File>();
for (final File fileIter : file.listFiles()) {
if (fileIter.isDirectory())
loadAll(fileIter);
else
ls.add(fileIter);
}
return ls.toArray();}
Now call this function--
File[] files = GET_ALL_RESOURCE_FILES('src/test/resources/');
Upvotes: 0