commons-IO

导入classpath

加入classpath的第三方jar包内的class文件才能在项目中使用

1.创建lib文件夹

2.将commons-io.jar拷贝到lib文件夹

3.右键点击commons-io.jar,Build Path→Add to Build Path

commons jar包下载

FilenameUtils类

这个工具类是用来处理文件名(译者注:包含文件路径)的,他可以轻松解决不同操作系统文件名称规范不同的问题

l  常用方法:

getExtension(String path):获取文件的扩展名;

getName():获取文件名;

isExtension(String fileName,String ext):判断fileName是否是ext后缀名;

FileUtils类

提供文件操作(移动文件,读取文件,检查文件是否存在等等)的方法。

l  常用方法:

readFileToString(File file):读取文件内容,并返回一个String;

writeStringToFile(File file,String content):将内容content写入到file中;

copyDirectoryToDirectory(File srcDir,File destDir);文件夹复制

copyFile(File srcFile,File destFile);文件复制

l  代码演示:

/*

* 完成文件的复制

*/

public class CommonsIODemo01 {

public static void main(String[] args) throws IOException {

//method1("D:\\test.avi", "D:\\copy.avi");

//通过Commons-IO完成了文件复制的功能

FileUtils.copyFile(new File("D:\\test.avi"), new File("D:\\copy.avi"));

}

//文件的复制

private static void method1(String src, String dest) throws IOException {

//1,指定数据源

BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));

//2,指定目的地

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));

//3,读

byte[] buffer = new byte[1024];

int len = -1;

while ( (len = in.read(buffer)) != -1) {

//4,写

out.write(buffer, 0, len);

}

//5,关闭流

in.close();

out.close();

}

}

/*

* 完成文件、文件夹的复制

*/

public class CommonsIODemo02 {

public static void main(String[] args) throws IOException {

//通过Commons-IO完成了文件复制的功能

FileUtils.copyFile(new File("D:\\test.avi"), new File("D:\\copy.avi"));

//通过Commons-IO完成了文件夹复制的功能

//D:\基础班 复制到 C:\\abc文件夹下

FileUtils.copyDirectoryToDirectory(new File("D:\\基础班"), new File("C:\\abc"));

}

}

最新文章

  1. DBSCAN——python实现
  2. java 日历代码实现
  3. nrf51822裸机教程-GPIO
  4. geotools
  5. telnet与ssh有什么不同呀
  6. UVa1589 象棋
  7. Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) A. Slime Combining 水题
  8. wxWidgets Tutorial
  9. 学习笔记——Java包装类
  10. 在线上服务器上无管理员权限下升级NodeJS版本
  11. BZOJ2839 集合计数 容斥
  12. 华为oj之字符串最后一个单词的长度
  13. Lightgbm 随笔
  14. JS解决在提交form表单时某个值不存在 alter弹窗点确定不刷新界面
  15. Android路径之Javascript基础-笔记
  16. POJ2676 Sudoku 舞蹈链 DLX
  17. webservice的model层命名空间不同的问题
  18. Docker(十一)-Docker commit创建镜像
  19. Install MySQL 5.7 on Fedora 25/24, CentOS/RHEL 7.3/6.8/5.11
  20. 使用vue.js常见错误之一

热门文章

  1. layui table渲染和数据处理
  2. python数据分析工具——Pandas、StatsModels、Scikit-Learn
  3. JDBC处理CLOB 和 BLOB大对象
  4. CG-CTF(6)
  5. [Qt] QString 常用函数
  6. linux sort 命令实用手册
  7. eclipse安装Axis2插件和简单的webservice发布
  8. mac OS 安装淘宝npm镜像
  9. How to get binary string from ArrayBuffer?
  10. 51 NOD 1049 最大子段和 动态规划 模板 板子 DP