我的目录中包含1000多个文件,我需要根据月份对它们进行压缩,重命名并将压缩后的文件放在文件夹中。我通常会手动执行此操作,但是我厌倦了这种方式。我编写了一个程序来重命名文件并将其放置在新文件夹中,但是我不知道如何在Windows 10上按月过滤或使用Java压缩文件。
String path = "C:\\\\Users\\\\srs\\\\Desktop\\\\Test\\notProcessed";
File[] filelist = new File(path).listFiles();
for (File file : filelist) {
Date d = new Date(file.lastModified());
Calendar c = Calendar.getInstance();
c.setTime(d);
int iyear = c.get(Calendar.YEAR);
int imonth = c.get(Calendar.MONTH);
String syear = Integer.toString(iyear);
String smonth = Integer.toString(imonth);
System.out.println(syear + "_" + smonth);
String destpath = "C:\\\\Users\\\\srs\\\\Desktop\\\\Test\\notProcessed\\\\TestZip\\\\";
byte[] buffer = new byte[1024];
try {
FileOutputStream fos = new FileOutputStream(destpath + syear + "_" + smonth + ".zip");
ZipOutputStream zos = new ZipOutputStream(fos);
System.out.println("Output to Zip : " + file);
System.out.println("File Added : " + file.getabsolutePath().toString());
ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer,len);
}
in.close();
zos.closeEntry();
zos.close();
System.out.println("Done");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
这就是我到目前为止所拥有的。该程序运行,但是没有给我所需的结果。它应该给我3个标有(基于lastModified()
的zip文件夹2019_07、2019_08、2019_09,但我得到的是2019_06、2019_07、2019_08、2019_10,每个文件夹中只有1个文件。