13 Replies Latest reply on Jan 9, 2008 8:27 AM by 807601

    File reading from multiple directory

    807601
      Hi,
      I have variable no. of directory in which varible no of file can exist.
      Say I have 3 directory.
      I want to read 1st file from 1st directory then 1st file from 2nd directory and then 1st file from 3rd directory then again 2nd file from 1st directory....so on.That is why I want to put the files in a arraylist in the same order that I have described.

      my code :
      String[] src_dirname = {"D:\\move","D:\\move1","D:\\move"};
                
      ArrayList filelist = new ArrayList();
      System.out.println(" No. of Input Directory = "+src_dirname.length);
      String stngfile = null;
      for (int i = 0; i < src_dirname.length; i++) {
           File directory = new File ((src_dirname).trim());
           String [] file = directory.list();
           System.out.println(" DIRECTORY :: == "+directory);     
           for (int j = 0; j < file.length; j++) {          
           stngfile = (src_dirname[i]).trim() + "/" +file[i];
           filelist.add(stngfile);
           }      
      }
      String [] files = (String[])filelist.toArray(new String[filelist.size()]);
      for (int i = 0; i < files.length; i++) {
      System.out.println("Input FILES ==>>> "+files[i]);
      }

      But that does not serve my purpose.
      any help is much apreciated. thanks
        • 1. Re: File reading from multiple directory
          807601
          What about subdirectories - do you want to read files from them as well.

          Also what do you mean by saying 1st file, 2nd file - what is the ordering (by name, size, extension, etc)?

          Did you run the code posted? I really doubt that as it will not compile.

          Clarify this. Without that it's really pointless to say anything more.

          Regards,
          • 2. Re: File reading from multiple directory
            807601
            You can suppose no subdirectory.In my actual code I have used FilenameFilter that will list files of that directory.
            The ordering is by Name (of the same type of file) as the system does the sequece of the file in the directory.

            Yes I did run the code that I have posted.

            I want to read first file from move folder,then first file from move1 folder,then first file from move2 folder..then again second file from move,second file from move1,then second file from move2...then again third file from move...until file of that folder will finish.

            Regds

            Edited by: more_java on Jan 4, 2008 1:56 AM
            • 3. Re: File reading from multiple directory
              807601
              Ok, here's sth to get you more than started:
              public List<File> getFilesFromDirs(String[] directories) {
              
                        List<List<File>> lOfFiles = new ArrayList<List<File>>();
                        List<File> allTheFilesInDirs = new ArrayList<File>();
              
                        for (String str : directories) {
                             File dir = new File(str);
                             if (!dir.exists() || !dir.isDirectory()) {
                                  continue;
                             }
              
                             List<File> filesInADir = new ArrayList<File>();
                             File files[] = dir.listFiles((FileFilter) null); // !!here use your FileFilter implementation
              
                             if (files != null) {
                                  Collections.addAll(filesInADir, dir.listFiles());
                             }
              
                             lOfFiles.add(filesInADir);
                        }
              
                        int index = -1;
              
                        while (true) {
                             index++;
                             boolean atLeastOneFound = false;
                             for (List<File> l : lOfFiles) {
                                  if (index < l.size()) {
                                       allTheFilesInDirs.add(l.get(index));
                                       atLeastOneFound = true;
                                  }
                             }
              
                             if (!atLeastOneFound) {
                                  break;
                             }
                        }
              
                        return allTheFilesInDirs;
                   }
              • 4. Re: File reading from multiple directory
                807601
                that worked, thanks a lot

                If I use this code with Java 4(J2SE 1.4) then how to do this?

                null
                • 5. Re: File reading from multiple directory
                  807601
                  follow-up:
                  • 6. Re: File reading from multiple directory
                    masijade
                    more_java wrote:
                    If I use this code with Java 4(J2SE 1.4) then how to do this?
                    Don't use Generics?
                    • 7. Re: File reading from multiple directory
                      807601
                      It doesn't support by my installed JDK

                      Regds
                      • 8. Re: File reading from multiple directory
                        masijade
                        In other words, in case you didn't understand (which you didn't), remove the Generics from the code, the rest can stay the way it is (although will probably have to cast a few things).

                        Try compiling a few times, the compiler will tell you what you have to change (except for the casting when rectreiving from the ArrayList).
                        • 9. Re: File reading from multiple directory
                          807601
                          for (String str : directories) {

                          --this is also not working in J2SE 1.4

                          Regds
                          • 10. Re: File reading from multiple directory
                            masijade
                            more_java wrote:
                            for (String str : directories) {

                            --this is also not working in J2SE 1.4

                            Regds
                            Well, that's a for loop right? So what do you think it does? And how do you right for loops otherwise? I mean, come on, That code snippet is not large, and simply using the compiler messages and some common sense, you can have it converted in a few minutes.

                            I could understand not wanting to do this with 5 megs worth of code or something, but come on, that's like what 30 lines, maybe. And at worst, half of them have an easy to fix "error" in them.
                            • 11. Re: File reading from multiple directory
                              807601
                              masijade. wrote:
                              Well, that's a for loop right? So what do you think it does? And how do you right for loops otherwise? I mean, come on, That code snippet is not large, and simply using the compiler messages and some common sense, you can have it converted in a few minutes.
                              I could understand not wanting to do this with 5 megs worth of code or something, but come on, that's like what 30 lines, maybe. And at worst, half of them have an easy to fix "error" in them.
                              I'm telling you; there's jinx associated with anyone who has "Java" in their user name. It's possessed I say!
                              • 12. Re: File reading from multiple directory
                                807601
                                The ordering is by Name (of the same type of file) as the system does the sequece of
                                the file in the directory.
                                From the API: "There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order."

                                http://java.sun.com/javase/6/docs/api/java/io/File.html#listFiles()

                                It may well wrok, but Java does the ordering not "the system", and Java makes no guarantees.
                                • 13. Re: File reading from multiple directory
                                  807601
                                  I've solve that problem using your given algorithem.Thanks very much to all of you,