zoukankan      html  css  js  c++  java
  • IO流之 commons-IO

     commons-IO

    导入classpath

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

    创建lib文件夹

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

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

     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完成了文件夹复制的功能
    

      

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    C# 遍历TreeView所有节点
    Oracle创建删除用户、角色、表空间、导入导出、...命令总结
    order by,group by和having的使用
    C/C++ 数据结构与算法笔记
    使用PS随意更换相片底色
    微软SQL Server2012增加对Hadoop的支持
    MOSS2010事件接收器开发以及自定义错误提示页
    将 SharePoint 开发与其他形式的开发进行比较
    ItemAdding事件接收器中无法取到【创建者】的字段的值
    ItemAdding实现数据验证中文字段,properties.AfterProperties值为null的问题
  • 原文地址:https://www.cnblogs.com/lxx2014/p/9543194.html
Copyright © 2011-2022 走看看