• Java之Apache Commons-IO使用精讲


    Commons IO是针对开发IO流功能的工具类库。
    主要包括六个区域:

    工具类——使用静态方法执行共同任务
    输入——用于InputStream和Reader实现
    输出——用于OutputStream和Writer实现
    过滤器——各种文件过滤器实现
    比较器——各种文件的java.util.Comparator实现
    文件监听器——监听文件系统事件的组件

    工具类

    IOUtils

    该工具类可能是平时使用得最多的工具类了。
    IOUtils包含处理读、写和复制的工具方法。方法对InputStream、OutputStream、Reader和Writer起作用。

    例如,从一个URL读取字节的任务,并且打印它们:

        public static void main(String[] args) throws Exception {
            //从网络上读取一个网页资源
            InputStream in = new URL("http://commons.apache.org").openStream();
            try {
                InputStreamReader inR = new InputStreamReader(in);
                BufferedReader buf = new BufferedReader(inR);
                String line;
                while ((line = buf.readLine()) != null) {
                    System.out.println(line);
                }
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        }
    //结果:
    //控制台打印出了这个网页的所有内容

    使用IOUtils:

        public static void main(String[] args) throws Exception {
            //从网络上读取一个网页资源
            try (InputStream in = new URL("http://commons.apache.org").openStream()) {
                System.out.println(IOUtils.toString(in, StandardCharsets.UTF_8));
            }
            //finally {
            //    IOUtils.closeQuietly(in);
            //}
        }

    主要方式介绍:

    buffer:一句话可以吧inputStream、outputStream、Reader、Witter等包装成带缓冲区的流,提高效率
    closeQuietly:可以关闭各种流、socket等任何closeable的实例(不过官方推荐使用try-with-resources来代替)
    contentEquals:比较两个InputStream或者两个Reader里面的内容(字节流)是否完全相同

        public static void main(String[] args) throws Exception {
            try (InputStream in1 = new URL("http://commons.apache.org").openStream(); InputStream in2 = new URL("http://commons.apache.org").openStream()) {
                System.out.println(in1.equals(in2)); //false
                System.out.println(IOUtils.contentEquals(in1, in2)); //true
            }
        }

    备注:contentEqualsIgnoreEOL(final Reader input1, final Reader input2) 该方法会忽略ignoring EOL characters

    copy:流的互相拷贝。可以将输入流拷到输出流。copy(final InputStream input, final OutputStream output, final int bufferSize),Reader拷贝到Writer等等
    copyLarge:当你的流拷贝的是大文件(一般大于2G级别),请使用此方法拷贝
    lineIterator:BufferedReader 通常在只有读到空格或者换行符时才会结束读取,攻击者很容易构内存攻击导致系统瘫痪,出于安全考虑这里推荐使用io包的LineIterator,并且其在性能上也优于普通流。

    lineIterator(final InputStream input, final Charset encoding)
    lineIterator(final InputStream input, final String encoding)
    lineIterator(final Reader reader)
        public static void main(String[] args) throws Exception {
            try (InputStream in1 = new URL("http://commons.apache.org").openStream()) {
                LineIterator lineIterator = IOUtils.lineIterator(in1, StandardCharsets.UTF_8);
                while (lineIterator.hasNext()) {
                    lineIterator.nextLine();
                }
                lineIterator.close();
            }
        }
    read、readFully:把输入流的东西读取添加到第二个参数中的字节数组里
    readLines:不解释
    resourceToByteArray、resourceToString:直接传入一个文件的路径,读取进来
    toBufferedInputStream:把普通的inputStream转换成带缓冲区的,返回一个新的InputStream
    toByteArray:吧输入流转换到字节数组
    toCharArray:
    toInputStream:吧字符、字符串等等直接读到流里
    toString:强大的方法,可以吧各种输出流读成一个串
    write、writeChunked、writeLines:把传入的字节数组,写入到输出流里(可指定编码)

    各种常用的常量:

    public static final char DIR_SEPARATOR_UNIX = '/';
    public static final char DIR_SEPARATOR_WINDOWS = '\';
    /**
        * The system directory separator character.  系统文件夹的分隔符 通用的
        */
       public static final char DIR_SEPARATOR = File.separatorChar;
        /**
        * The Unix line separator string.  换行符
        */
       public static final String LINE_SEPARATOR_UNIX = "
    ";
       //winows换行符
       public static final String LINE_SEPARATOR_WINDOWS = "
    ";
        /**
        * The system line separator string.  通用的换行符
        */
       public static final String LINE_SEPARATOR;

    FilenameUtils

    FilenameUtils类包含工具方法不需要使用File对象就可以操作文件名。该类致力于屏蔽Unix和Windows之间的不同,避免这些环境之间的转换(例如,从开发到生产)。 开发在windows、生产在Linux

    一般使用较少,这里不做过多介绍.

    FileSystemUtils:2.6版本已经废弃。推荐使用JDK自己的FileStore代替

  • 相关阅读:
    iPhone 12 Pro 不锈钢边框刮伤 All In One
    .vscodeignore All In One
    程序员如何挑选和购买一款高性价比的电动升降桌 All In One
    TypeScript function arguments destructuring All In One
    LeetCode 旋转数组算法题解 All In One
    python 中实现DNA一致性序列计算
    python中输出两条长度一致序列碱基不同的个数
    python 中统计不同scafflod的GC含量并输出GC含量最高的scafflod
    python 中 斐波那契兔子问题
    python 中如何将列表中的数值转换为字符串
  • 原文地址:https://www.cnblogs.com/deityjian/p/11404937.html
Copyright © 2020-2023  润新知