1 8 package org.jivesoftware.util.log.output.io.rotate; 9 10 import java.io.File ; 11 12 25 public class RevolvingFileStrategy implements FileStrategy { 26 27 private int maxCount; 29 30 private String baseFileName; 32 33 public RevolvingFileStrategy(final String baseFileName, final int maxCount) { 34 35 this.baseFileName = baseFileName; 36 this.maxCount = maxCount; 37 38 if (-1 == this.maxCount) { 39 this.maxCount = 5; 40 } 41 } 42 43 public File currentFile() { 44 return new File (baseFileName); 45 } 46 47 52 public File nextFile() { 53 for (int i = maxCount; i > 0; i--) { 55 File test = new File (baseFileName.substring(0, baseFileName.lastIndexOf('.')) + 56 "_" + i + baseFileName.substring(baseFileName.lastIndexOf('.'))); 57 58 if (i == maxCount && test.exists()) { 59 test.delete(); 60 } 61 62 if (test.exists()) { 63 File r = new File (baseFileName.substring(0, baseFileName.lastIndexOf('.')) + 64 "_" + (i + 1) + baseFileName.substring(baseFileName.lastIndexOf('.'))); 65 test.renameTo(r); 66 } 67 } 68 69 File current = new File (baseFileName); 71 File first = new File (baseFileName.substring(0, baseFileName.lastIndexOf('.')) + 72 "_1" + baseFileName.substring(baseFileName.lastIndexOf('.'))); 73 current.renameTo(first); 74 75 return new File (baseFileName); 77 } 78 } 79 80 | Popular Tags |