复制文件

来源:互联网 发布:大智慧下载数据在哪里 编辑:程序博客网 时间:2024/05/03 11:28
public static void copy(File file, File dest) throws FileNotFoundException {if (!file.exists()) {throw new FileNotFoundException(file.getName() + " not found");}if (file.isDirectory()) {dest.mkdirs();return;}if (!file.isFile()) {return;}FileInputStream in = null;FileOutputStream out = null;try {in = new FileInputStream(file);out = new FileOutputStream(dest);byte[] buffer = new byte[1024];int count = 0;while ((count = in.read(buffer)) != -1) {if (!isCanceled)out.write(buffer, 0, count);else break;}in.close();out.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (in != null) {in.close();}if (out != null) {out.flush();out.close();}} catch (IOException e) {e.printStackTrace();}}}