io - Can't read the files from a folder in series in java -
i trying read files folder java, found snippet , used it.
file folder = new file("z.."); file[] listoffiles = folder.listfiles(); (int = 0; < listoffiles.length; i++) { file file = listoffiles[i]; if (file.isfile() && file.getname().endswith(".txt")) { string content = fileutils.readfiletostring(file); }
this works fine except doesn't retrieve files in order. have files numbered file 0,file 1, file2.....file10 , how retrieves file 0 file 1 file 10 , file 2, should retrieve in proper series. should using else? i'm new this.
there great example of using custom comparator sort array of files here: best way list files in java, sorted date last modified
you have modify return of comparator along lines of
return f1.getname().compareto(f2.getname());
and should you're looking for.
if files numbered may want compare file names integers:
return integer.valueof(f1.getname()).compareto(integer.valueof(f2.getname()));
Comments
Post a Comment