Java实现文件拷贝

来源:互联网 发布:统计局房价数据 编辑:程序博客网 时间:2024/05/16 11:14
package com.knight.file;import java.io.*;public class Demo02 {public static void main(String[] args) {File src=new File("E:/java_mr/test");File dest=new File("E:/java_mr/copy");copyDir(src,dest);}public static void copyDir(File src,File dest){if(src.isFile()){try {copyFile(src,dest);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else if(src.isDirectory()){dest.mkdir();for(File temp:src.listFiles()){copyDir(temp,new File(dest,temp.getName()));}}}public static void copyFile(File src,File dest) throws IOException,FileNotFoundException {//File src=new File(srcPath);//File dest=new File(destPath);InputStream is=new FileInputStream(src);OutputStream os=new FileOutputStream(dest);byte []flush=new byte[1024];int len=0;while(-1!=(len=is.read(flush))){os.write(flush, 0, len);}os.flush();os.close();is.close();}}

0 0
原创粉丝点击