zoukankan      html  css  js  c++  java
  • 【commons】IO工具类——commons-io之IOUtils

    本文转载自xingoo:

      https://www.cnblogs.com/xing901022/p/5978989.html

    一、常用静态变量

    public static final char DIR_SEPARATOR_UNIX = '/';
    public static final char DIR_SEPARATOR_WINDOWS = '\';
    public static final char DIR_SEPARATOR;
    public static final String LINE_SEPARATOR_UNIX = "
    ";
    public static final String LINE_SEPARATOR_WINDOWS = "
    ";
    public static final String LINE_SEPARATOR;
    
    
    static {
        DIR_SEPARATOR = File.separatorChar;
        
        StringBuilderWriter buf = new StringBuilderWriter(4);
        PrintWriter out = new PrintWriter(buf);
        out.println();
        LINE_SEPARATOR = buf.toString();
        out.close();
    }

    二、常用方法 

      copy

      这个方法可以拷贝流,算是这个工具类中使用最多的方法了。支持多种数据间的拷贝:

    copy(inputstream,outputstream)
    copy(inputstream,writer)
    copy(inputstream,writer,encoding)
    copy(reader,outputstream)
    copy(reader,writer)
    copy(reader,writer,encoding)

      copy内部使用的其实还是copyLarge方法。因为copy能拷贝Integer.MAX_VALUE的字节数据,即2^31-1。、

      copyLarge类似,不过适合于拷贝2G的大数据

      以下主要方法不再赘述,参考上文链接

      完整API请参考官方API 

      read

        从一个流中读取内容

      readFully

        这个方法会读取指定长度的流,如果读取的长度不够,就会抛出异常

      readLines

        readLines方法可以从流中读取内容,并转换为String的list

      skip

        这个方法用于跳过指定长度的流,

      skipFully

        这个方法类似skip,只是如果忽略的长度大于现有的长度,就会抛出异常

      write

        这个方法可以把数据写入到输出流中

      writeLines

        这个方法可以把string的List写入到输出流中

      close

        关闭URL连接

      closeQuietly

        忽略nulls和异常,关闭某个流

      contentEquals

        比较两个流是否相同

      contentEqualsIgnoreEOL

        比较两个流,忽略换行符

      lineIterator

        读取流,返回迭代器

      toBufferedInputStream

        把流的全部内容放在另一个流中

      toBufferedReader

        返回输入流

      toByteArray

        返回字节数组

      toCharArray

        返回字符数组

      toInputStream

        返回输入流

      toString

        返回字符串

  • 相关阅读:
    (转)排序算法——归并排序与递归
    在serviceImpl里使用自身的方法
    hibernate manytoone中的lazy EAGER
    autowire异常的三个情况
    (转)js的call和apply
    mysql创建外键出错(注意数据库表字段排序)
    easyui datagrid
    ava.lang.NullPointerException的一般解决方法
    spring简单事务管理器
    关于使用注解出现BeanCreationException或者NameNotFoundException的解决方法
  • 原文地址:https://www.cnblogs.com/jiangbei/p/8384486.html
Copyright © 2011-2022 走看看