码迷,mamicode.com
首页 > Web开发 > 详细

Apache commons io 概要介绍

时间:2014-12-18 17:04:00      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:apache   java   io   code   

首先贴一段Apache commons IO官网上的概要介绍

Commons IO 2.4 API

Packages

org.apache.commons.io

This package defines utility classes for working with streams, readers, writers and files.

org.apache.commons.io.comparator

This package provides various Comparator implementations for Files.

org.apache.commons.io.filefilter

This package defines an interface (IOFileFilter) that combines both FileFilter and FilenameFilter.

org.apache.commons.io.input

This package provides implementations of input classes, such as InputStream and Reader.

org.apache.commons.io.monitor

This package provides a component for monitoring file system events (directory and file create, update and delete events).

org.apache.commons.io.output

This package provides implementations of output classes, such as OutputStream and Writer.

 时代发展很快,时间过的更快,我eclipse里面很多的commons io还是1.2版本。现在commons的主流版本是2.4.

         先写点代码热热身。

packagetest.ffm83.commons.io;

 

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileWriter;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.Writer;

importjava.net.URL;

 

importorg.apache.commons.io.FileUtils;

importorg.apache.commons.io.IOUtils;

importorg.apache.commons.lang.StringUtils;

/**

 * commonsio 的一些简单基本用法

 * @author范芳铭

 */

public classIOBaseUsage {

 

    publicstatic void main(String[] args) throws Exception {

        //getInputCopyToWrite();

        //writeStringToFile();

        getUrlWriteToFile();

    }

   

    /**

     * 输入流复制到输出流

     * @author 范芳铭

     */

    privatestatic void getInputCopyToWrite() throws Exception{   

        InputStreamins = new FileInputStream(new File("D:\\ffm83\\ffm83.txt"));

        Writerwrite = new FileWriter("D:\\ffm83\\write.txt");

        try{

            IOUtils.copy(ins,write);

            System.out.println(StringUtils.center("输入流复制到输出流成功",50, "-"));

        }catch(IOException e){

            System.out.println(StringUtils.center("输入流复制到输出流失败",50, "-"));

            e.printStackTrace();

        }  

        write.close();

        ins.close();   

    }

   

    /**

     * 字符输出到指定文本

     * @author 范芳铭

     */

    privatestatic void writeStringToFile() throws Exception

        Stringstr = "字符输出到指定文本,作者范芳铭。";

        Filefile = new File("D:\\ffm83\\ffm83.txt");

        try{

            FileUtils.writeStringToFile(file,str,"UTF-8");

            System.out.println(StringUtils.center("字符输出到指定文本成功",50, "-"));

        }catch(IOException e){

            System.out.println(StringUtils.center("字符输出到指定文本失败",50, "-"));

            e.printStackTrace();

        }  

    }

   

    /**

     * 获取url流,转换成String

     * @author 范芳铭

     */

    privatestatic void getUrlWriteToFile() throws Exception { 

        URLurl = new URL("http://blog.csdn.net/ffm83/article/details/42000885");

        InputStreamips = url.openStream();

        StringurlContent = "";

        try{

            urlContent= IOUtils.toString(ips,"UTF-8");

            System.out.println(urlContent);//也可以利用writeStringToFile保留到文本

            System.out.println(StringUtils.center("获取url流,转换成String成功",50, "-"));

        }catch(IOException e){

            System.out.println(StringUtils.center("获取url流,转换成String失败",50, "-"));

            e.printStackTrace();

        }finally{

            IOUtils.closeQuietly(ips);

        }  

    }

}

Apache commons io 概要介绍

标签:apache   java   io   code   

原文地址:http://blog.csdn.net/ffm83/article/details/42006057

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!