6 Replies Latest reply: Mar 11, 2008 7:20 AM by 807591 RSS

    Storing the last modified date for files in a HashMap

    807591
      This method gets a list of all the .java files in the source subdirectory, gets the last modified date for them and stores the file name and last modified date in a HashMap.
      import java.util.HashMap;
      import java.io.FilenameFilter;
      import java.io.File;
      import java.util.ArrayList;
      
      public class myClass
      {
           public static void main(String[] args)
           {
                HashMap result = getLastModified();
           }
      
           static FilenameFilter javaFilter = new FilenameFilter()
           {
              public boolean accept(File dir, String name)
              {
                  return name.endsWith(".java");
              }
          };
      
           public HashMap getLastModified()
           {
                HashMap lastModified = new HashMap();
                ArrayList javaFiles = new ArrayList();
                File sourceDir = new File(".\\Source");
                File currentFile;
                long lLastModified;
                
                //get a list of all the .java files in the source directory
                javaFiles=listFiles(javaFilter);
                
                //for all .java files get and store the last modified date
                for(int i=0; i<javaFiles.size(); i++)
                {
                     currentFile=(File)javaFiles.get(i);
                     lLastModified=currentFile.lastModified();
                     
                     lastModified.put(currentFile, lLastModified);
                }
                
                return lastModified;
           }
      }
      The problems I am getting are:
      >
      The method listFiles(FilenameFilter) is undefined for the type myClass

      and
      >
      The method put(Object, Object) in the type HashMap is not applicable for the arguments (File, long)

      can anyone help? What's the best way to go aobut this?
        • 1. Re: Storing the last modified date for files in a HashMap
          807591
          Well, the method listFiles does not exist, at least not in the part you pasted. Maybe you just forgot sourceDir.listFiles()
          If you are using JDK 5 or newer you should declare HashMap using generics. So instead of HashMap you would write
          HashMap<File, Long>
          .
          Just my opinion, but is it really necessary to store File objects together with their last modification date as any File object contains that date. So it would be enough to keep the files in the array returned by listFiles or in an ArrayList and just call lastModified() whenever you need that information.

          Edited by: abenstex on Mar 11, 2008 12:47 PM
          • 2. Re: Storing the last modified date for files in a HashMap
            807591
            What do you mean it doesn't exist? It's right here
            http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#listFiles(java.io.FileFilter)
            also, I would use Generics but the code needs to be 1.4.2 compatible.
            • 3. Re: Storing the last modified date for files in a HashMap
              807591
              I know that it's a part of the File class but you have to use a File object to call it and in your code you just call listFiles.
              See here:
              javaFiles=listFiles(javaFilter);
              should be
              javaFiles=someFileObject.listFiles(javaFilter);
              Edited by: abenstex on Mar 11, 2008 12:51 PM
              • 4. Re: Storing the last modified date for files in a HashMap
                807591
                Thanks for the replies abenstex I modified it so that javaFiles is now a File[] and made the change you suggested but I still have the second problem, here's the code so far:
                import java.util.HashMap;
                import java.io.FilenameFilter;
                import java.io.File;
                import java.util.ArrayList;
                 
                public class myClass
                {
                     public static void main(String[] args)
                     {
                          HashMap result = getLastModified();
                     }
                 
                     static FilenameFilter javaFilter = new FilenameFilter()
                     {
                        public boolean accept(File dir, String name)
                        {
                            return name.endsWith(".java");
                        }
                    };
                 
                     public HashMap getLastModified()
                     {
                          HashMap lastModified = new HashMap();
                          File[] javaFiles = {};
                          File sourceDir = new File(".\\Source");
                          File currentFile;
                          long lLastModified;
                          
                          //get a list of all the .java files in the source directory
                          javaFiles=sourceDir.listFiles(javaFilter);
                          
                          //for all .java files get and store the last modified date
                          for(int i=0; i<javaFiles.length; i++)
                          {
                               currentFile=(File)javaFiles;
                               lLastModified=currentFile.lastModified();
                               
                               lastModified.put(currentFile, lLastModified);
                          }
                          
                          return lastModified;
                     }
                }
                • 5. Re: Storing the last modified date for files in a HashMap
                  807591
                  Maybe you didn't read before so let me just repeat: just my opinion, but is it really necessary to store File objects together with their last modification date as any File object contains that date by default. So it would be enough to keep the files in the array returned by listFiles or in an ArrayList and just call lastModified() whenever you need that information.
                  Anyway, instead of writing:
                  lastModified.put(currentFile, lLastModified);
                  try
                  lastModified.put(currentFile, new Long(lLastModified));
                  that should fix it I guess, but i have been using JDK5 for years now and don't remember the pre-generics time very well ;-).
                  • 6. Re: Storing the last modified date for files in a HashMap
                    807591
                    Damn, why didn't I think of that? Thanks abenstex you get the Dollars.