Swing/BackupFile のバックアップの現在との差分(No.16)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/BackupFile へ行く。
- 1 (2004-02-26 (木) 02:48:53)
- 2 (2004-06-02 (水) 09:51:10)
- 3 (2004-06-04 (金) 07:25:54)
- 4 (2004-06-17 (木) 05:51:39)
- 5 (2004-08-31 (火) 12:42:42)
- 6 (2004-10-08 (金) 06:17:33)
- 7 (2004-11-04 (木) 10:02:21)
- 8 (2005-04-28 (木) 04:32:27)
- 9 (2005-06-28 (火) 03:16:59)
- 10 (2005-11-11 (金) 19:45:01)
- 11 (2006-02-27 (月) 15:27:04)
- 12 (2006-04-12 (水) 19:33:52)
- 13 (2006-05-30 (火) 10:35:57)
- 14 (2006-05-30 (火) 14:28:00)
- 15 (2007-03-13 (火) 00:24:02)
- 16 (2007-10-11 (木) 12:50:03)
- 17 (2010-12-13 (月) 00:01:58)
- 18 (2013-02-26 (火) 14:49:44)
- 19 (2014-10-10 (金) 14:08:13)
- 20 (2015-01-16 (金) 21:15:19)
- 21 (2016-08-11 (木) 21:30:47)
- 22 (2016-08-17 (水) 18:47:38)
- 23 (2017-10-07 (土) 17:57:25)
- 24 (2018-01-23 (火) 13:20:30)
- 25 (2018-04-04 (水) 17:15:41)
- 26 (2018-07-11 (水) 16:40:42)
- 27 (2020-07-10 (金) 16:57:27)
- 28 (2021-12-14 (火) 18:52:02)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:Backup Fileを番号付きで作成 #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 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTH9enrSII/AAAAAAAAASA/du4XRgNsIZs/s800/BackupFile.png) **概要 [#h2d72f39] [[xyzzy]]風の番号付きバックアップファイルを作成します。 * サンプルコード [#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); } }} #screenshot * 解説 [#explanation] 上記のサンプルでは、`xyzzy`風の番号付きバックアップファイル作成のテストを行っています。 **サンプルコード [#dbcb1005] - 以下の例では古いバージョンを`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~`が新にバックアップとして生成 #code{{ 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); } File tmpFile = File.createTempFile("tmp", ".foo~", file.getParentFile()); File file = makeBackupFile(file, 2, 3); tmpFile.renameTo(file); }} //-&jnlp; -&jar; -&zip; **解説 [#xf457df5] 上記のサンプルでは、[[xyzzy]]風の番号付きバックアップのテストを行っています。 * 参考リンク [#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)] 以下の例では、古いバージョンを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~ が新にバックアップとして生成される File tmpFile = File.createTempFile("tmp", ".foo~", file.getParentFile()); File file = makeBackupFile(file, 2, 3); tmpFile.renameTo(file); **コメント [#c2bd4ac8] * コメント [#comment] #comment #comment