Swing/BackupFile のバックアップの現在との差分(No.16)
TITLE:Backup Fileを番号付きで作成
Backup Fileを番号付きで作成
編集者:Terai Atsuhiro~
作成日:2003-10-07
更新日:2021-12-14 (火) 18:52:02
概要
拡張子に番号を付けたバックアップファイルを作成します。Screenshot
Advertisement
概要
xyzzy風の番号付きバックアップファイルを作成します。サンプルコード
#spanend
#spanadd
private File makeBackupFile(File file, int intold, int intnew) {
#spanend
File testFile = null;
String newfilename = file.getAbsolutePath();
if (intold == 0 && intnew == 0) {
file.delete();
return new File(newfilename);
}
boolean testFileFlag = false;
for (int i = 1; i <= intold; i++) {
testFile = new File(file.getParentFile(), file.getName() + "." + i + "~");
if (!testFile.exists()) {
testFileFlag = true;
break;
}
}
if (!testFileFlag) {
for (int i = intold + 1; i <= intold + intnew; i++) {
testFile = new File(file.getParentFile(), file.getName() + "." + i + "~");
if (!testFile.exists()) {
testFileFlag = true;
break;
}
}
}
if (testFileFlag) {
System.out.println("createBKUP1" + testFile.getAbsolutePath());
file.renameTo(testFile);
} else {
File tmpFile3 = new File(file.getParentFile(),
file.getName() + "." + (intold + 1) + "~");
tmpFile3.delete();
for (int i = intold + 2; i <= intold + intnew; i++) {
File tmpFile1 = new File(file.getParentFile(),
file.getName() + "." + i + "~");
File tmpFile2 = new File(file.getParentFile(),
file.getName() + "." + (i - 1) + "~");
tmpFile1.renameTo(tmpFile2);
}
File tmpFile = new File(file.getParentFile(),
file.getName() + "." + (intold + intnew) + "~");
System.out.println("changeBKUP2" + tmpFile.getAbsolutePath());
file.renameTo(tmpFile);
}
// System.out.println(newfilename);
return new File(newfilename);
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin#screenshot
解説
上記のサンプルでは、xyzzy
風の番号付きバックアップファイル作成のテストを行っています。
サンプルコード
- 以下の例では古いバージョンを
2
つ、新しいバージョンを3
つバックアップとして残す-
tmp.foo.1~
からtmp.foo.5~
が残る -
tmp.foo.1~
がもっとも古いバージョンのファイルになる
-
- バックアップファイルが
5
個以上になった場合、古い方のバージョン2
つはそのまま残して新しいバージョンをずらしたバックアップが更新される-
tmp.foo.1~
,tmp.foo.2~
は残る -
tmp.foo.3~
は削除 -
tmp.foo.4~
はtmp.foo.3~
にリネーム -
tmp.foo.5~
はtmp.foo.4~
にリネーム -
tmp.foo.5~
が新にバックアップとして生成
-
private File makeBackupFile(File file, int intold, int intnew) {
File testFile = null;
String newfilename = file.getAbsolutePath();
if(intold==0 && intnew==0) {
file.delete();
return new File(newfilename);
}
boolean testFileFlag = false;
for(int i=1;i<=intold;i++) {
testFile = new File(file.getParentFile(), file.getName()+"."+i+"~");
if(!testFile.exists()) {
testFileFlag = true;
break;
}
}
if(!testFileFlag) {
for(int i=intold+1;i<=intold+intnew;i++) {
testFile = new File(file.getParentFile(), file.getName()+"."+i+"~");
if(!testFile.exists()) {
testFileFlag = true;
break;
}
}
}
if(testFileFlag) {
System.out.println("createBKUP1"+testFile.getAbsolutePath());
file.renameTo(testFile);
}else{
File tmpFile3 = new File(file.getParentFile(),
file.getName()+"."+(intold+1)+"~");
tmpFile3.delete();
for(int i=intold+2;i<=intold+intnew;i++) {
File tmpFile1 = new File(file.getParentFile(),
file.getName()+"."+i+"~");
File tmpFile2 = new File(file.getParentFile(),
file.getName()+"."+(i-1)+"~");
tmpFile1.renameTo(tmpFile2);
}
File tmpFile = new File(file.getParentFile(),
file.getName()+"."+(intold+intnew)+"~");
System.out.println("changeBKUP2"+tmpFile.getAbsolutePath());
file.renameTo(tmpFile);
}
//System.out.println(newfilename);
return new File(newfilename);
}
#spanadd
File tmpFile = File.createTempFile("tmp", ".foo~", file.getParentFile());
#spanend
#spanadd
File file = makeBackupFile(file, 2, 3);
#spanend
#spanadd
tmpFile.renameTo(file);
#spanend
- &jar;
- &zip;
解説
上記のサンプルでは、xyzzy風の番号付きバックアップのテストを行っています。参考リンク
以下の例では、古いバージョンを2つ、新しいバージョンを3つバックアップとして残します。- tmp.foo.1~ から tmp.foo.5~ が残される
- tmp.foo.1~ がもっとも古いバージョンのファイルになる
- tmp.foo.1~, tmp.foo.2~ は残される
- tmp.foo.3~は削除される
- tmp.foo.4~ はtmp.foo.3~にリネーム
- tmp.foo.5~ はtmp.foo.4~にリネーム
- tmp.foo.5~ が新にバックアップとして生成される
File tmpFile = File.createTempFile("tmp", ".foo~", file.getParentFile()); File file = makeBackupFile(file, 2, 3); tmpFile.renameTo(file);