home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 4427 / aisp_memo-20090510.7z / hed_dat3_src / src / FileUtils.java < prev    next >
Encoding:
Java Source  |  2009-04-18  |  2.8 KB  |  103 lines

  1. import java.io.*;
  2. import java.nio.channels.*;
  3.  
  4. class FileUtils {
  5.     public static final String BACKUP_EXT = ".backup";
  6.  
  7.     /**
  8.      * ├▒░∞ñ╬Ñ╒ÑíÑñÑδñ≥Ñ│Ñ╘í╝ñ╣ñδ
  9.      * @param src    Ñ│Ñ╘í╝╕╡Ñ╒ÑíÑñÑδ
  10.      * @param dst    Ñ│Ñ╘í╝└ΦÑ╒ÑíÑñÑδ(Ñ╟ÑúÑ∞Ñ»Ñ╚ÑΩñ╧╗╪─Ω╔╘▓─)
  11.      * @param preserve    ║╟╜¬╣╣┐╖╞ⁿñ≥╩▌╗²ñ╣ñδñ½ñ╬Ñ╒ÑΘÑ░
  12.      */
  13.     public static void copyFile(File src, File dst, boolean preserve) throws IOException {
  14.     FileChannel srcChan = new FileInputStream(src).getChannel();
  15.     FileChannel destChan = new FileOutputStream(dst).getChannel();
  16.     srcChan.transferTo(0, srcChan.size(), destChan);
  17.     srcChan.close();
  18.     destChan.close();
  19.  
  20.     // ║╟╜¬╣╣┐╖╞ⁿñ≥Ñ│Ñ╘í╝
  21.     if(preserve)
  22.         dst.setLastModified(src.lastModified());
  23.     }
  24.  
  25.     /**
  26.      * Ñ╒ÑíÑñÑδñ≥║╞╡ó┼¬ñ╦Ñ│Ñ╘í╝ñ╣ñδ
  27.      * @param src    Ñ│Ñ╘í╝╕╡
  28.      * @param dst    Ñ│Ñ╘í╝└Φ
  29.      * @param preserve    ║╟╜¬╣╣┐╖╞ⁿñ≥╩▌╗²ñ╣ñδñ½ñ╬Ñ╒ÑΘÑ░
  30.      */
  31.     public static void copyRecursive(File src, File dst, boolean preserve) throws IOException {
  32.     if(src.isFile()) {
  33.         copyFile(src, dst, preserve);
  34.         return;
  35.     }
  36.  
  37.     if(src.isDirectory()) {
  38.         String[] entries = src.list();
  39.         for(int i = 0; i < entries.length; i++) {
  40.         File src2 = new File(src, entries[i]);
  41.         File dst2 = new File(dst, entries[i]);
  42.         if(src2.isFile())
  43.             copyFile(src2, dst2, preserve);
  44.         else if(src2.isDirectory())
  45.             copyRecursive(src2, dst2, preserve);
  46.         else
  47.             throw new RuntimeException(src2.getPath()
  48.                            + " is not file nor directory");
  49.         }
  50.     }
  51.  
  52.     throw new RuntimeException(src.getPath()
  53.                    + " is not file nor directory");
  54.     }
  55.  
  56.     /**
  57.      * ║╟╜¬╣╣┐╖╗■╣∩ñ≥╩▌╗²ñ╖ñ╞Ñ╨Ñ├Ñ»ÑóÑ├Ñ╫ñ≥║ε└«ñ╣ñδ
  58.      */
  59.     public static void makeBackupFile(File file) throws IOException {
  60.     makeBackupFile(file, true);
  61.     }
  62.  
  63.     /**
  64.      * Ñ╒ÑíÑñÑδñ╬Ñ╨Ñ├Ñ»ÑóÑ├Ñ╫ñ≥║ε└«ñ╣ñδ
  65.      */
  66.     public static void makeBackupFile(File file, boolean preserve) throws IOException {
  67.     if(!file.exists())
  68.         return; // Ñ¬ÑΩÑ╕Ñ╩Ñδñ¼┬╕║▀ñ╖ñ╩ñññ╩ñΘ▓┐ñΓñ╖ñ╩ññ
  69.  
  70.     File backup = new File(file.getPath() + BACKUP_EXT);
  71.     if(backup.exists())
  72.         return; // Ñ╨Ñ├Ñ»ÑóÑ├Ñ╫ñ¼┬╕║▀ñ╣ñδñ╩ñΘ▓┐ñΓñ╖ñ╩ññ
  73.  
  74.     copyFile(file, backup, preserve);
  75.     }
  76.  
  77.     /**
  78.      * Ñ╒ÑíÑñÑδñ≥╢⌡ñ╦└┌ñΩ╡═ñßñδíú
  79.      * ñ│ñ╬╗■ñ╦ñ▐ñ└Ñ╨Ñ├Ñ»ÑóÑ├Ñ╫ñ╡ñ∞ñ╞ñ╩ñ½ñ├ñ┐ñΘíóÑ╨Ñ├Ñ»ÑóÑ├Ñ╫ñΓ║ε└«ñ╣ñδíú
  80.      */
  81.     public static void truncate(File file) throws IOException {
  82.     File backup = new File(file.getPath() + BACKUP_EXT);
  83.     if(backup.isFile()) {
  84.         file.delete();
  85.         touch(file);
  86.     } else {
  87.         file.renameTo(backup);
  88.         touch(file);
  89.     }
  90.     }
  91.  
  92.     /**
  93.      * Ñ╒ÑíÑñÑδñ╬║╟╜¬╣╣┐╖╗■╣∩ñ≥╣╣┐╖ñ╣ñδíúÑ╒ÑíÑñÑδñ¼┬╕║▀ñ╖ñ╩ññ╛∞╣τñ╧║ε└«ñ╣ñδíú
  94.      */
  95.     public static void touch(File file) throws IOException {
  96.     if(!file.exists())
  97.         (new FileOutputStream(file)).close();
  98.     if(!file.isFile())
  99.         return; // Ñ╬í╝Ñ┐Ñ├Ñ┴
  100.     file.setLastModified((new java.util.Date()).getTime());
  101.     }
  102. }
  103.