There are a lot of way to copy file in Java.
I have wrote sample codes for them.
I think Java 7 way will be the normal & standard way soon!!
I have wrote sample codes for them.
I think Java 7 way will be the normal & standard way soon!!
import static com.dukesoftware.utils.common.ExceptionUtils.throwNotImplementedException; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.net.URI; import java.nio.channels.FileChannel; import java.nio.file.Paths; import org.apache.commons.io.FileUtils; import com.google.common.io.Closeables; import com.google.common.io.Files; public enum Copier { GUAVA{ @Override public void copy(File src, File dest) throws IOException { Files.copy(src, dest); } }, JAVA7{ @Override public void copy(File src, File dest) throws IOException { java.nio.file.Files.copy(src.toPath(), dest.toPath(), REPLACE_EXISTING); } @Override public void copy(String src, String dest) throws IOException { java.nio.file.Files.copy(Paths.get(src), Paths.get(dest), REPLACE_EXISTING); } }, FILE_CHANNEL{ @Override public void copy(File src, File dest) throws IOException { FileChannel inChannel = null; FileChannel outChannel = null; try { inChannel = new FileInputStream(src).getChannel(); outChannel = new FileOutputStream(dest).getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); } finally { Closeables.closeQuietly(inChannel); Closeables.closeQuietly(outChannel); } } }, LEGACY{ @Override public void copy(File src, File dest) throws IOException { try (FileOutputStream fos = new FileOutputStream(dest)){ Copier.copy(new FileInputStream(src), fos, _1K_BYTES); } } public void copy(String inName, String outName) throws IOException{ BufferedInputStream is = new BufferedInputStream(new FileInputStream(inName)); BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(outName)); Copier.copy(is, os, _1K_BYTES); } }, COMMONS_IO{ @Override public void copy(File src, File dest) throws IOException { FileUtils.copyFile(src, dest); } }; public static final int _1K_BYTES = 1024; public void copy(File src, File dest) throws IOException{ throwNotImplementedException(); } public void copy(String src, String dest) throws IOException{ throwNotImplementedException(); } public static void copyDirectory(File src, File dest) throws IOException{ if(src.isDirectory()){ if(!dest.exists()){ if(!dest.mkdir()){ throw new IOException("Could not create directory:" + dest); } } for (String file : src.list()) { copyDirectory(new File(src, file), new File(dest, file)); } }else{ GUAVA.copy(src, dest); } } public static int determinBufSize(File srcFile) { long len = srcFile.length(); if(len > Integer.MAX_VALUE){ return Integer.MAX_VALUE; } else{ return (int)len; } } public static void copyNoClose(InputStream in, int buffSize, OutputStream os) throws IOException { byte[] buffer = new byte[buffSize]; for (int bytes = 0 ;(bytes = in.read(buffer)) != -1; ) { os.write(buffer, 0, bytes); } os.flush(); } // legacy java copy methods public final static void copy(InputStream is, OutputStream os, int bufsize) throws IOException { copy(is, os, new byte[bufsize]); } public final static void copy(InputStream is, OutputStream os) throws IOException { copy(is, os, new byte[_1K_BYTES]); } final static void copy(InputStream is, OutputStream os, byte[] buffer)throws IOException { try{ for (int bytes = 0 ;(bytes = is.read(buffer)) != -1; ) { os.write(buffer, 0, bytes); } os.flush(); }finally{ Closeables.closeQuietly(is); } } public final static void copy(Reader reader, Writer writer, char[] buf) throws IOException{ try{ for(int b = 0; (b = reader.read(buf)) != -1;){ writer.write(buf, 0, b); } writer.flush(); } finally{ Closeables.closeQuietly(reader); } } public static final void copy(URI in, File out) throws IOException { try (FileOutputStream fos = new FileOutputStream(out)){ Copier.copy(in.toURL().openStream(), fos, _1K_BYTES); } } }
コメント