Here you can find the source of copyFile(String strSrc, String strDest)
Parameter | Description |
---|---|
strSrc | source file |
strDest | destination file |
// ////////////////////////////////////////////////////// public static boolean copyFile(String strSrc, String strDest)
//package com.java2s; import java.io.*; public class Main { public static final int BUFFER_SIZE = 65536; /**/*from w ww . j a va2s .co m*/ * Copy file from src to des, override if des exist * * @param strSrc * source file * @param strDest * destination file * @return true if succees, otherswise false * @author Thai Hoang Hiep */ // ////////////////////////////////////////////////////// public static boolean copyFile(String strSrc, String strDest) { FileInputStream isSrc = null; FileOutputStream osDest = null; try { File flDest = new File(strDest); if (flDest.exists()) flDest.delete(); File flSrc = new File(strSrc); if (!flSrc.exists()) return false; isSrc = new FileInputStream(flSrc); osDest = new FileOutputStream(flDest); byte btData[] = new byte[BUFFER_SIZE]; int iLength; while ((iLength = isSrc.read(btData)) != -1) osDest.write(btData, 0, iLength); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { safeClose(isSrc); safeClose(osDest); } } /** * Close object safely * * @param is * InputStream */ // ////////////////////////////////////////////////////// public static void safeClose(InputStream is) { try { if (is != null) is.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Close object safely * * @param os * OutputStream */ // ////////////////////////////////////////////////////// public static void safeClose(OutputStream os) { try { if (os != null) os.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Close object safely * * @param fl * RandomAccessFile */ // ////////////////////////////////////////////////////// public static void safeClose(RandomAccessFile fl) { try { if (fl != null) fl.close(); } catch (Exception e) { e.printStackTrace(); } } }