java - Unable to Access files in Directory -
i have directory structure follows:
db_set -d1 - db_1.txt -d2 - db_2.txt -d3 - db_3.txt -d4 - db_4.txt -d5 - db_5.txt
i want store db_1.txt
, db_2.txt
, db_3.txt
, db_4.txt
, db_5.txt
in arraylist
. how can this? partial code:
file folder = new file("./webcontent/datasets/db_set/"); file[] listoffiles = folder.listfiles(); system.out.println("listoffiles: "+listoffiles); arraylist<file> sub_dir = new arraylist<file>(); (int = 0; < listoffiles.length; i++) { if (listoffiles[i].isfile()) { system.out.println("file " + listoffiles[i].getname()); } else if (listoffiles[i].isdirectory()) { sub_dir.add(listoffiles[i]); } }
you need go 2 levels deep.
file folder = new file("./webcontent/datasets/db_set/"); file[] listofsubdirectories = folder.listfiles(new filefilter() { @override public boolean accept(file file) { return file.isdirectory(); } }); arraylist<file> fileslist = new arraylist<file>(); (file dir : listofsubdirectories) { file[] files = dir.listfiles(new filefilter() { @override public boolean accept(file file) { return file.isfile(); } }); (file f : files) { fileslist.add(f); } }