[concurrency-interest] Help

edsen at libero.it edsen at libero.it
Mon Jan 23 03:31:10 EST 2012


Hello,
 
I need orders my files from list directory, that is changing continously.
My files must be fetched by order-date (oldest from newest...) and I check the age of files from file naming convention. The files are:XXXXX_yymmdd.
Below you can find the code that get the following input parameters: 
 
- this.directory: directory of file list.
- fileRegexp: regular expression in order to retrieve the date.
- datetimeGroup: group where the date is present in the file naming convention.
- timestampFromFileNameFormat: date format convention.
 


// get the list of files currently in the directory



File[] files = 

this.dirListByDate(this.directory,fileRegexp,datetimeGroup,timestampFromFileNameFormat);
 
 






























protected  File[] dirListByDate(final File folder, final Pattern fileRegexp, final int datetimeGroup, final String timestampFromFileNameFormat) throws IOException {



 
if  (!folder.isDirectory() || !folder.exists()) {


throw new
 IOException(folder.getName() +  " : Not a folder or not exist");
}
 
File files[] = this.list(folder, false); 
// don't include subfolder
Arrays.sort(files, newComparator<Object>() {


public int compare(final Object o1, final Object o2) {
 


final String s1 = ((File) o1).getName();


final String s2 = ((File) o2).getName();



final  Matcher m = fileRegexp.matcher(s1);


final  Matcher n = fileRegexp.matcher(s2);



 
if  (m.matches() && n.matches()) {


final String date1 = m.group(datetimeGroup);


final String date2 = n.group(datetimeGroup);



final long dateAndTime1 = Strings.setDateTimeFromFileName(date1, timestampFromFileNameFormat);


final long dateAndTime2 = Strings.setDateTimeFromFileName(date2, timestampFromFileNameFormat);


 
return dateAndTime1 < dateAndTime2 ? -1 : dateAndTime1 > dateAndTime2 ? 1 : 0;
}

else
{


log.error("Files don't match...File1->" + s1 + " File2 -> " + s2);


alarm.sendAlarm(AlarmInterface.FILE_NAMING_CONVENTION_FAILED,"File Naming Convention incorrect: "
+
s1 + 

" with size " + s1.getBytes() + " of directory " + folder + s2 + 

" with size " + s2.getBytes() + " of directory "+ folder);


return 0;
}
}
});


return files;
 
 














protected File[] list(File folder, boolean includeSubFolder) {


 
if  (!folder.isDirectory()) {


return null;
}
 
File files[] = folder.listFiles();
List<File> list = new ArrayList<File>();


 
for (File file : files) {


 
if  (file.isDirectory()) {


if (includeSubFolder) {
list.add(file); 
  }
} 

else
 {
list.add(file);
}
}
 


if(list.isEmpty()) {


return list.toArray(newFile[] {});
}


return list.toArray(new File[] {});
}
 
What happens? Happens that every thing works for 3-4 traffic days, but sometimes it processes newest files instead of oldest.
I don't understand what can be wrong??
Can you help me in order to fix it?
Can you suggest me a solution?
 
Thanks in advance.
Kind regards.
 
 
 
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120123/2fdeccc8/attachment.html>


More information about the Concurrency-interest mailing list