0
package codes;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.security.acl.LastOwnerException;
import java.text.SimpleDateFormat;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ModifyAndDelete 
{
private static final String FOLDER_PATH = "C:\\Users\\s13w63\\Desktop\\Zip";

public static void main(String[] args) throws IOException
{
    File dir = new File(FOLDER_PATH);

    File[] files = dir.listFiles(new FilenameFilter() 
    {

        @Override
        public boolean accept(File directory, String fileName)
        {
            if (fileName.endsWith(".INC")) 
            {
                return true;
            }
            return false;
        }
    });

    SimpleDateFormat sdf = new SimpleDateFormat("MM");

    for(File f:files)
    {
        String month=sdf.format(f.lastModified());

        final int lastModifiedMonth=Integer.parseInt(month);        
    }       

    }       
}

This is to get the month of the file in which it is modified lastly. Now I want to get the files in a list according to month. How to get the files according to month and I need list for each month. Please help me out guys...

I am using the below code to add and iterate using a hashmap, but It was listing only one file for each month.

Map<Integer, List<File>> map = new HashMap<Integer, List<File>>();

    for (File f : files) {
        String month = sdf.format(f.lastModified());

        final int lastModifiedMonth = Integer.parseInt(month);

        map.put(lastModifiedMonth, new ArrayList<File>());

        map.get(lastModifiedMonth).add(f);
    }

    for (Entry<Integer, List<File>> entry : map.entrySet()) 
    {
        System.out.println(entry.getKey() + "/" + entry.getValue());
    }

2 Answers2

0

Something like that:

Map<Integer, List<File>> map = new HashMap<>();
for(File f:files)
{
    String month=sdf.format(f.lastModified());
    final int lastModifiedMonth=Integer.parseInt(month);        
    if (!map.containsKey(lastModifiedMonth)) {
        map.put(lastModifiedMonth, new ArrayList<>());
    }
    map.get(lastModifiedMonth).add(f);
}       
// use map

(typed here, so might not compile)

0

Since you are using Java 7, at least use java.nio.file. File is terminally broken (note that .listFiles() can return null and you don't even check for this).

Sorting by months is easy enough... Code:

final Map<Integer, List<Path>> filesByMonths = new HashMap<>();

final Calendar cal = new Calendar();

final Path dir = Paths.get(FOLDER_PATH);

final DirectoryStream<Path> dirStream 
    = Files.newDirectoryStream(dir, "*.INC");

List<Path> list;
int month;

for (final Path entry: dir) {
    cal.setTimeInMillis(Files.getLastModifiedTime(entry).toMillis());
    month = cal.get(Calendar.MONTH);
    list = filesByMonth.get(month);
    if (list == null)
        filesByMonth.put(month, list = new ArrayList<>());
    list.add(entry);
}

You may also want to use a TreeMap instead of a HashMap if you want to iterate by month.

As to creating the zips, well you didn't tell, but then with Java 7 you can manipuate a zip as a FileSystem (see the link I posted above, it provides an example). In fact you could create by advance ALL FileSystems to all zips and just copy the files directly into them, no need at all to go through an intermediate map.

fge
  • 119,121
  • 33
  • 254
  • 329