• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*Backup Fileを番号付きで作成 [#da6c5825]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2003-10-07~
更新日:&lastmod;
---
category: swing
folder: BackupFile
title: Backup Fileを番号付きで作成
tags: [File]
author: aterai
pubdate: 2003-11-03T02:48:53+09:00
description: 拡張子に番号を付けたバックアップファイルを作成します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTH9enrSII/AAAAAAAAASA/du4XRgNsIZs/s800/BackupFile.png
---
* 概要 [#summary]
拡張子に番号を付けたバックアップファイルを作成します。

#contents
**概要 [#h2d72f39]
[[xyzzy]]風の番号付きバックアップファイルを作成します。
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTH9enrSII/AAAAAAAAASA/du4XRgNsIZs/s800/BackupFile.png)

**サンプルコード [#dbcb1005]
 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) {
     Logger.global.info("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)+"~");
     Logger.global.info("changeBKUP2"+tmpFile.getAbsolutePath());
     file.renameTo(tmpFile);
   }
   //Logger.global.info(newfilename);
   return new File(newfilename);
 }
* サンプルコード [#sourcecode]
#code(link){{
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);
}
}}

* 解説 [#explanation]
上記のサンプルでは、`xyzzy`風の番号付きバックアップファイル作成のテストを行っています。

**解説 [#xf457df5]
[[xyzzy]]風の番号付きバックアップでは、古いバージョンと新しいバージョンを幾つずつ残すかを指定することができます。以下のサンプルでは、古いバージョンを2つ、新しいバージョンを3つ残し、バックアップファイルが5個以上になったら、新しいバージョンをずらして保存するようにしています。
- 以下の例では古いバージョンを`2`つ、新しいバージョンを`3`つバックアップとして残す
-- `tmp.foo.1~`から`tmp.foo.5~`が残る
-- `tmp.foo.1~`がもっとも古いバージョンのファイルになる

 File tmpFile = File.createTempFile("fontemp", "fo~", file.getParentFile());
 //UXMLDocument.saveElement(elm, tmpFile, "Shift_JIS", true, 2);
 //int iold = 0;
 //int inew = 0;
 //if(prefs.getBoolean("flag_bkup", true)) {
 //  iold = prefs.getInt("bkup_old", 2);
 //  inew = prefs.getInt("bkup_new", 3);
 //}
 file = makeBackupFile(file, 2, 3);
 tmpFile.renameTo(file);
- バックアップファイルが`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~`が新にバックアップとして生成

**コメント [#c2bd4ac8]
#code{{
File tmpFile = File.createTempFile("tmp", ".foo~", file.getParentFile());
File file = makeBackupFile(file, 2, 3);
tmpFile.renameTo(file);
}}

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/io/File.html#createTempFile-java.lang.String-java.lang.String-java.io.File- File#createTempFile(...) (Java Platform SE 8)]

* コメント [#comment]
#comment
#comment