i have foldername.txt file has list of folders path. how can total count of total number of files present inside folder path.
for 1 path i'm able this.
new file("folder").listfiles().length. but problems i'm not able read path foldername.txt file .
i'm trying
file objfile = objpath.tofile(); try(bufferedreader in = new bufferedreader( new filereader(objfile))){ string line = in.readline(); while(line != null){ string[] linesfile = line.split("\n"); but when i'm trying access linesfile array i'm getting exception. linesfile[1] exception in thread "main" java.lang.arrayindexoutofboundsexception:.
my question why i'm getting java.lang.arrayindexoutofboundsexception ?. , how read individual directory path , total number of files inside it. there way read total number of files in subdirectory too.
foldername.txt has structure this.
e:/folder1
e:/folder2
exception because :
readline() method reads entire line input removes newline characters it.so it's unable split around \n
here code want,.
i have folderpath.txt has list of directory this.
d:\305
d:\deployment
d:\heapdumps
d:\program files
d:\programming
this code gives want + can modify according need
public class main { public static void main(string args[]) throws ioexception { list<string> folderspath = new arraylist<string>(); file folderpathfile = new file("c:\\users\\ankur\\desktop\\folderpath.txt"); /** * read folderpath.txt , path , store * folderspath list */ bufferedreader reader = new bufferedreader(new filereader(folderpathfile)); string line = reader.readline(); while(line != null){ folderspath.add(line); line = reader.readline(); } reader.close(); /** * map path(i.e folder) total no of * files present in path (i.e folder) */ map<string, integer> nooffilesinfolder = new hashmap<string, integer>(); (string pathoffolder:folderspath){ file[] files2 = new file(pathoffolder).listfiles();//get arrays of files int totalfilescount = files2.length;//get total no of files present nooffilesinfolder.put(pathoffolder,totalfilescount); } system.out.println(nooffilesinfolder); } } output:
{d:\program files=1, d:\heapdumps=16, d:\deployment=48, d:\305=4, d:\programming=13}
edit : counts total number of files present inside subdirectory too.
/**this counts * total number of files present inside subdirectory too. */ map<string, integer> nooffilesinfolder = new hashmap<string, integer>(); (string pathoffolder:folderspath){ int filescount = 0; file[] files2 = new file(pathoffolder).listfiles();//get arrays of files (file f2 : files2){ if (f2.isdirectory()){ filescount += new file(f2.tostring()).listfiles().length; } else{ filescount++; } } system.out.println(filescount); nooffilesinfolder.put(pathoffolder, filescount); } system.out.println(nooffilesinfolder); }
Comments
Post a Comment