• Apache Common-IO 使用


    Apache Common-IO 是什么?

      Apache File 工具类,能够方便的操作 File

    运行环境

      jdk 1.7

      commons-io 2.6

    测试代码

     1 package com.m.basic;
     2 
     3 import org.apache.commons.io.FileUtils;
     4 import org.junit.Test;
     5 
     6 import java.io.File;
     7 import java.io.IOException;
     8 import java.net.URL;
     9 
    10 public class FileUtilTest {
    11 
    12     String path = "D:/malin/xxx/file.txt";
    13 
    14     @Test
    15     public void makeFileTest() throws IOException {
    16         final File file = new File("D:/malin/sss/");
    17         FileUtils.forceMkdir(file);
    18     }
    19 
    20     @Test
    21     public void forceMkdirParentTest() throws IOException {
    22         File file = new File(path);
    23         FileUtils.forceMkdirParent(file);
    24         if (!file.createNewFile()) {
    25             String message = "Unable to create File " + path;
    26             throw new IOException(message);
    27         } else {
    28             System.out.println("create success");
    29         }
    30     }
    31 
    32     @Test
    33     public void writingFileTest() throws IOException {
    34         File file = new File(path);
    35         for (int i = 0; i < 3; i++)
    36             FileUtils.writeStringToFile(file, "Hello World" + System.getProperty("line.separator"), "UTF-8", true);
    37     }
    38 
    39     @Test
    40     public void readingFIleTest() throws IOException {
    41         File file = new File(path);
    42         String content = FileUtils.readFileToString(file, "utf-8");
    43         System.out.println(content);
    44     }
    45 
    46     @Test
    47     public void copyingFileTest() throws IOException {
    48         File srcFile = new File(path);
    49         File destFile = new File("D:/malin/sss/file.txt");
    50         FileUtils.copyFile(srcFile, destFile);
    51     }
    52 
    53     @Test
    54     public void deletingFileTest() throws IOException {
    55         FileUtils.deleteDirectory(new File(path).getParentFile());
    56     }
    57 
    58     @Test
    59     public void convertingTest() throws IOException {
    60         FileUtils.copyURLToFile(new URL("http://www.baidu.com"), new File(path));
    61     }
    62 }
    View Code

    参考

    http://commons.apache.org/proper/commons-io/description.html

  • 相关阅读:
    【STL】各容器成员对比表
    windows笔记页文件支持的内存映射文件
    windows笔记【内核对象线程同步】线程同步对象速查表
    windows笔记虚拟内存
    windows笔记使用内存映射文件在进程之间共享数据
    svn个人服务端
    解决安装Visual Studio .NET 2003 时FrontPage 2000 WEB 扩展客户端 安装失败
    vc6.0转vs2008连接错误
    Sublime Text插件推荐
    末日了,说出你的梦想、愿望还有遗憾吧。
  • 原文地址:https://www.cnblogs.com/linma/p/7826226.html
Copyright © 2020-2023  润新知