0

I have noticed a weird behavior when listing files in a directory. My situation is i have a list of txt files which i save in database and corresponding to those data files there are xml file that contains some tags for each txt which are associated as their meta-properties in db.

When i list files in from directory and read xml file some how the stream is changing and listing the file again which has already been added to db. Filenames are id in my db and are unique,re-listing them violates the primary key constraint.

i wanted to know why does the listing gets disturbed?

try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(folderReader,filter){
    for (Path file : directoryStream) {
         // Read XML
         // add txt file to database
         // attach meta props to that file
    }
    }catch(IOException ex){}

Now for some file it runs fine but then some how the files that had already been listed are coming back again. Like 1.txt was processed and saved in db but in the loop later on it is listed again.

Adyz
  • 302
  • 4
  • 18
  • I am using dom4j for reading xml. – Adyz Jun 09 '14 at 07:52
  • Not really sure what you mean by stream is changing? Can you post your code in pastebin or somewhere. – Rajarshi Goswami Jun 09 '14 at 07:59
  • i have also found this line in documentation. "The iterator is weakly consistent. It is thread safe but does not freeze the directory while iterating, so it may (or may not) reflect updates to the directory that occur after the DirectoryStream is created." – Adyz Jun 09 '14 at 08:00

1 Answers1

1

Your last comment seems to describe exactly what happens here.

I guess the DirectoryStream adds (or adds not) newly saved files without checking for their prior listing.

In this case, it's probably a good idea to add the entries from the DirectoryStream to another Iterable or even Set (e.g. HashSet) before handling them and check if they are were in the Set before (Set#add return's boolean for that reason).

Community
  • 1
  • 1
Franz Ebner
  • 4,951
  • 3
  • 39
  • 57