Java File atomic copy

Description

Java File atomic copy


import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
   public static void main(String[] argv) throws Exception {
      File paramFile1 = new File("Main.java");
      File paramFile2 = new File("Main.java.bak");
      atomicCopy(paramFile1, paramFile2);
   }//from   w  ww .  j av a2s .  co m

   public static void atomicCopy(File paramFile1, File paramFile2) throws IOException {
      FileInputStream localFileInputStream = null;
      FileOutputStream localFileOutputStream = null;
      File localFile = null;
      try {
         localFile = new File(paramFile2.getPath() + ".tmp");
         localFileInputStream = new FileInputStream(paramFile1);
         localFileOutputStream = new FileOutputStream(localFile);
         localFileInputStream.getChannel().transferTo(0L, paramFile1.length(), localFileOutputStream.getChannel());
         localFileOutputStream.close();
         if (!localFile.renameTo(paramFile2))
            throw new IOException("Failed to rename " + localFile + " to " + paramFile2);
         System.out.println("Copied " + paramFile1 + " to " + paramFile2);
      } catch (IOException localIOException) {
         close(localFileOutputStream);
         delete(paramFile2);
         throw localIOException;
      } finally {
         close(localFileInputStream);
         close(localFileOutputStream);
         delete(localFile);
      }
   }

   public static void close(Closeable paramCloseable) {
      try {
         if (paramCloseable != null)
            paramCloseable.close();
      } catch (Throwable localThrowable) {
         localThrowable.printStackTrace();;
      }
   }

   public static boolean delete(File paramFile) {
      if ((paramFile != null) && (paramFile.exists())) {
         if (!paramFile.delete()) {
            System.out.println("Failed to delete file " + paramFile);
            return false;
         }
         System.out.println("Deleted file " + paramFile);
      }
      return true;
   }
}


//package com.demo2s;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] argv) throws Exception {
        File from = new File("Main.java");
        File to = new File("Main.java.bak");
        atomicCopyFile(from, to);//from   w  ww . j av a2  s  .  com
    }

    public static void atomicCopyFile(File from, File to) {
        FileInputStream in = null;
        FileOutputStream out = null;
        File tmp = null;
        try {
            tmp = new File(to.getPath() + ".tmp");
            in = new FileInputStream(from);
            out = new FileOutputStream(tmp);
            in.getChannel().transferTo(0, from.length(), out.getChannel());
            out.close();
            if (!tmp.renameTo(to)) {
                throw new IOException("Failed to rename " + tmp + " to " + to);
            }
        } catch (IOException x) {
            close(out);
            deleteFile(to);
        } finally {
            close(in);
            close(out);
            deleteFile(tmp);
        }
    }

    public static void close(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (Throwable x) {
            x.printStackTrace();
        }
    }

    public static void deleteFile(File file) {

        if (file.isFile()) {
            file.delete();
            return;
        }

        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }

            for (int i = 0; i < childFiles.length; i++) {
                deleteFile(childFiles[i]);
            }
            file.delete();
        }
    }
}



PreviousNext

Related