4 import java.io.FilenameFilter;
5 import java.io.IOException;
7 public class BackupFilenameFilter implements FilenameFilter
12 public String template;
16 public String extension;
18 public BackupFilenameFilter(String base, String template, int digits,
22 this.template = template;
24 this.extension = extension;
28 public boolean accept(File dir, String filename)
34 dir.getCanonicalPath() + File.separatorChar + filename);
35 if (file.isDirectory())
37 // backup files aren't dirs!
40 } catch (IOException e)
42 System.out.println("IOException when checking file '" + filename
43 + "' is a backupfile");
46 BackupFilenameParts bffp = new BackupFilenameParts(filename, base,
47 template, digits, extension);
48 ret = bffp.isBackupFile();
52 public static String getBackupFilename(int index, String base,
53 String template, int digits, String extension)
55 String numString = String.format("%0" + digits + "d", index);
56 String backupSuffix = template.replaceAll(BackupFiles.NUM_PLACEHOLDER,
58 String backupfilename = base + backupSuffix + extension;
59 return backupfilename;
64 class BackupFilenameParts
80 public BackupFilenameParts(File file, String base, String template, int digits,
83 this(file.getName(), base, template, digits, extension);
86 public BackupFilenameParts(String filename, String base, String template,
87 int digits, String extension)
89 this.isBackupFile = false;
91 // calculate minimum length of a backup filename
92 int minlength = base.length() + template.length()
93 - BackupFiles.NUM_PLACEHOLDER.length() + digits
96 if (!(filename.startsWith(base) && filename.endsWith(extension)
97 && filename.length() >= minlength))
103 int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
104 String templateStart = template;
105 String templateEnd = "";
106 if (numcharstart > -1)
108 templateStart = template.substring(0, numcharstart);
109 templateEnd = template.substring(
110 numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
113 int startLength = base.length() + templateStart.length();
114 int endLength = templateEnd.length() + extension.length();
115 String numString = filename.substring(startLength,
116 filename.length() - endLength);
118 if (filename.length() >= startLength + digits + endLength
119 && filename.startsWith(base + templateStart)
120 && filename.endsWith(templateEnd + extension)
121 && numString.matches("[0-9]+"))
124 this.templateStart = templateStart;
125 this.num = Integer.parseInt(numString);
126 this.digits = digits;
127 this.templateStart = templateStart;
128 this.templateEnd = templateEnd;
129 this.isBackupFile = true;
134 public boolean isBackupFile()
136 return this.isBackupFile;
139 public int indexNum()