设为首页 收藏本站
查看: 1245|回复: 0

[经验分享] apache FileUtils 和 IOUtils 工具类

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-2 12:39:52 | 显示全部楼层 |阅读模式
  不重复开发的轮子,既然人家已经写好了通用成熟的工具,与其自己吭哧吭哧写通用类,还不如拿来用即可。但是用归用,不了解还是不行滴,咻咻,
  apache下 开源了许多实用的工具类,一般都是在commons包下。
  开源的在线文档:http://commons.apache.org/
  commons.jar 包下载 :http://commons.apache.org/io/download_io.cgi
  这里讲到了:   IOUtils 和  FileUtils 。 中文文档奇缺,有知道的童鞋可以吱一声。
  1.IOUtils 简要说明
  该类为input/output操作提供了通用静态方法。
  包括功能:
  ·closeQuietly——这些关闭一个流的方法将忽略nulls和例外的情况。
  ·toXxx /read-这些方法从一个流读取数据。
  · write这些方法将数据写到一个流里。
  copy——这些方法用于从一个流的所有的数据复制到另一个流。
  ·contentEquals——这些方法用于比较两个流的内容。

  





byte-to-char方法集和char-to-byte方法集都包括一个转换步骤。给出的两种方法集合在每种情况下,一种是使用平台默认的编码方式,另一个允许你指定一个编码。
鼓励你总是指定一个编码因为依靠平台违约会带来意想不到的结果,例如从开发阶段移动到产品部署。
  





这个类里面所有的方法在读一个流的时候内部是被缓冲的。这意味着我们外部使用的时候就不需要BufferedInputStream或BufferedReader。在测试中默认缓存4 K已被证明是有效。





无论在何处,在这个类里的方法在这堂课里不要冲洗或关闭流。这是为了避免犯不可移植的假设那小溪的来源和进一步使用。因此调用者还需要负责使用后手动关闭流。

-------------------------------------------------------------------------------------------------------------------------------------------------------------

使用IOUtils来重写原来的读取写入流的操作,代码简洁,没了烦人的try  以前连基本的操作都要在两个流之间倒来倒去 还要考虑怎么转换,现在一切都是浮云了。
--------------------------------------------------------------------------------------------------------------------------------------------------------------
这边是API的简单介绍:
  1.closeQuietly 包括对各种类型输入输出流的关闭。 无条件的关闭流。






[java] view plaincopy

  • byte[] data = "Hello, World".getBytes();   
  •   
  • OutputStream out = null;  
  •   
  • try {   
  •   
  •     out = new FileOutputStream("foo.txt");   
  •   
  •     out.write(data);   
  •   
  •     //out.close();//close errors are handled  我们通常的用法,是不太好的。  
  •   
  •     }   
  •   
  • catch (IOException e) {   
  •   
  • // error handling   
  •   
  • } finally  
  •   
  • {  
  •   
  •   IOUtils.closeQuietly(out); 以后就直接在finally里关闭流。也不需要判断条件。  
  •   
  • }  
  
2.
toByteArray 从各类输入流中读取数据并转换为字节数组。
  toByteArray(参数) 里的参数包括:Reader,InputStream,String,这里String 已经过时,等同于String.getBytes();

toByteArray

public static byte[] toByteArray(InputStream input,
long size)
throws IOException
  以字节数组的形式来获取inputStream流里的内容,如果在流的大小已知的情况下,可以用此方法来代替默认的toByteArray(InputStream input)方法。
  注:该方法会检查size是否可以安全地转换为int,在其正式读到字节数组前。
  
  ①读操作
IOUtils类提供的读操作方法有两大类:第一类是readLines方法。第二类是toXxx方法。
※ readLines方法

List readLines(InputStream input)
List readLines(InputStream input, String encoding)
readLines(Reader input)
我们知道在字节流中是没有“行”的概念的,但是为什么这里的readLines方法可以接收InputStream呢?看看源代码就知道了




DSC0000.gif DSC0001.gif public static List readLines(InputStream input, String encoding) throws IOException DSC0002.gif {
DSC0003.gif DSC0004.gif         if (encoding == null) {
DSC0005.gif             return readLines(input);
        } else {
            InputStreamReader reader = new InputStreamReader(input, encoding);
            return readLines(reader);
DSC0006.gif         }
DSC0007.gif     }
DSC0008.gif
public static List readLines(Reader input) throws IOException {
        BufferedReader reader = new BufferedReader(input);
        List list = new ArrayList();
        String line = reader.readLine();
        while (line != null) {
            list.add(line);
            line = reader.readLine();
        }
        return list;
    }  
原来在底层,IOUtils使用了InputStreamReader对input
stream进行了包装,到了readLines(Reader)方法内,又再加了一个缓冲。如果我们是直接调用readLines(Reader)方
法,为了确保编码正确,需要手工创建一个InputStreamReader并指明encoding,否则将采用默认的encoding。
※ toXxx方法
IOUtils支持把inputStream中的数据转换成byte[],char[],String对象。而且input
stream可以是字节流,字符流。同时可以指定encoding。这些方法实质上是“输出”的过程:即从输入流中读入数据,然后转换为
byte[],char[],String,输出到内存中。看看下面的一个源代码:




    public static char[] toCharArray(InputStream is, String encoding)
            throws IOException {
        CharArrayWriter output = new CharArrayWriter();
        copy(is, output, encoding);
        return output.toCharArray();
    }

    public static void copy(InputStream input, Writer output, String encoding)
            throws IOException {
        if (encoding == null) {
            copy(input, output);
        } else {
          InputStreamReader in = new InputStreamReader(input, encoding);
            copy(in, output);
        }
    }

    public static int copy(Reader input, Writer output) throws IOException {
        long count = copyLarge(input, output);
        if (count > Integer.MAX_VALUE) {
            return -1;
        }
        return (int) count;
    }

    public static long copyLarge(Reader input, Writer output) throws IOException {
        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

  
我们可以看到这个过程是没有进行flush的操作的,也就是说使用者必须负责在调用结束后进行缓存清空和输入、输入流关闭。对于input stream是文件的情况,在FileUtils的文件读方法的最后都会调用IOUtils.closeQuietly(in);方法来确保输入流正确关闭。
②写操作

和读操作一样,IOUtils一样提供了大量的写方法,这些方法可以将byte[],char[],StringBuffer,String,Collection中的数据以字节流,字符流的形式写入到目的源。
※ writeLines方法




public static void writeLines(Collection lines, String lineEnding,
            OutputStream output, String encoding) throws IOException {
        if (encoding == null) {
            writeLines(lines, lineEnding, output);
        } else {
            if (lines == null) {
                return;
            }
            if (lineEnding == null) {
                lineEnding = LINE_SEPARATOR;
            }
            for (Iterator it = lines.iterator(); it.hasNext(); ) {
                Object line = it.next();
                if (line != null) {
                    output.write(line.toString().getBytes(encoding));
                }
                output.write(lineEnding.getBytes(encoding));
            }
        }
    }

public static void writeLines(Collection lines, String lineEnding,
            Writer writer) throws IOException {
        if (lines == null) {
            return;
        }
        if (lineEnding == null) {
            lineEnding = LINE_SEPARATOR;
        }
        for (Iterator it = lines.iterator(); it.hasNext(); ) {
            Object line = it.next();
            if (line != null) {
                writer.write(line.toString());
            }
            writer.write(lineEnding);
        }
    }  
如果我们查看FileUtils,会发现它对所有的文件读写(包括writeLines,writeStringToFile),都是调用字节流+encoding的方式来进行的。因为所有基于字符流的方式最终都需要转换为基于字节流的方式。

③流拷贝
我们在从文件等数据源读入数据时,习惯性地以字节读入,到了内存又转换成String对象,最后修改性地以字符写回文件。IOUtils提供了一系列方便的方法来进行这中间的转换。
copy(InputStream input, Writer output, String encoding),这个方法使用指定的encoding,从字节流中读入字节,然后按照encoding解码,通过字符流写回目的源。
copy(Reader input, OutputStream output, String encoding),这个方法从字符流中读取字符,使用指定的encoding编码,通过字节流写回目的源,然后立即清空缓冲。
上面这两个方法底层都调用了一个名为copyLarge的方法,他们分别在通过一个byte[]或者char[]数组对要写回的内容进行缓冲。一次性地从源端读入4K数据然后通过输出流写回。




    public static long copyLarge(InputStream input, OutputStream output)
            throws IOException {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

    public static long copyLarge(Reader input, Writer output) throws IOException {
        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-93320-1-1.html 上篇帖子: Apache Nutch 1.3 学习笔记三(Inject CrawlDB Reader) 下篇帖子: Win下apache出现问题:“No services installed” || apache服务启动
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表