博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件操作(把源文件拷贝到目标文件路径下)
阅读量:4708 次
发布时间:2019-06-10

本文共 1292 字,大约阅读时间需要 4 分钟。

1 /** 2      * 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下 3      *  4      * @param source 5      *            源文件 6      * @param target 7      *            目标文件路径 8      * @return void 9      */10     public static void copy(File source, File target) {11         File tarpath = new File(target, source.getName());12         if (source.isDirectory()) {13             tarpath.mkdir();14             File[] dir = source.listFiles();15             for (int i = 0; i < dir.length; i++) {16                 copy(dir[i], tarpath);17             }18         } else {19             try {20                 InputStream is = new FileInputStream(source); // 用于读取文件的原始字节流21                 OutputStream os = new FileOutputStream(tarpath); // 用于写入文件的原始字节的流22                 byte[] buf = new byte[1024];// 存储读取数据的缓冲区大小23                 int len = 0;24                 while ((len = is.read(buf)) != -1) {25                     os.write(buf, 0, len);26                 }27                 is.close();28                 os.close();29             } catch (FileNotFoundException e) {30                 e.printStackTrace();31             } catch (IOException e) {32                 e.printStackTrace();33             }34         }35     }

 

转载于:https://www.cnblogs.com/jishuixiansheng/archive/2012/06/30/2570977.html

你可能感兴趣的文章
最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
查看>>
C# 两个datatable中的数据快速比较返回交集或差集
查看>>
关于oracle样例数据库emp、dept、salgrade的mysql脚本复杂查询分析
查看>>
adb shell am 的用法
查看>>
iOS10 UI教程视图和子视图的可见性
查看>>
FindChildControl与FindComponent
查看>>
中国城市json
查看>>
android下载手动下载Android SDK
查看>>
C++学习:任意合法状态下汉诺塔的移动(原创)
查看>>
leetcode133 - Clone Graph - medium
查看>>
UNET学习笔记2 - 高级API(HLAPI)
查看>>
"ORA-00942: 表或视图不存在 "的原因和解决方法[转]
查看>>
Oauth支持的5类 grant_type 及说明
查看>>
C#中用DateTime的ParseExact方法解析日期时间(excel中使用系统默认的日期格式)
查看>>
W3100SM-S 短信猫代码发送 上
查看>>
netty接收大文件的方法
查看>>
软件工程设计之四则运算
查看>>
SpringMVC @ResponseBody 406
查看>>
Partial Tree UVALive - 7190(完全背包)
查看>>
Ubuntu安装搜狗拼音教程
查看>>