JavaEE

java commonsIO

2019-01-28  本文已影响1673人  会摄影的程序员

1 commons-IO

1.1 FileUtils

  1. readFileToString(File file); 读取文件内容,返回一个String
  1. writeStringToFile(File file, String content); 将字符串写入file中
  1. copyFile(File srcFile, FIle destFile); 文件复制
  1. copyDirectoryToDirectory(File srcDir,File destDir); 文件夹复制

demo:

public class CommonsDemo {
    String path = "";
    @Before
    public void getPath(){
        File file = new File("");
        path = file.getAbsolutePath()+"/src/com/looc/demo10/commonsIODemo";
    }


    @Test
    public void commWrit() throws IOException {
        FileUtils.writeStringToFile(new File(path+"/测试.txt"),"这里是测试第一行","utf-8",true);
    }


    @Test
    public void commRead() throws IOException{
        String readStr = FileUtils.readFileToString(new File(path+"/测试.txt"),"utf-8");
        System.out.println(readStr);
    }

    @Test
    public void commCopyFile() throws IOException{
        FileUtils.copyFile(new File("C:\\Users\\81022\\Downloads\\commons-io-2.6.jar"),new File(path+"/test.jar"));
    }

    @Test
    public void commCopyDirectory() throws IOException{
        FileUtils.copyDirectory(new File(""),new File(""));
    }

    @Test
    public void commDemo() throws IOException{
        FileUtils.forceMkdir(new File("C:\\Users\\81022\\Downloads\\aaa\\xxx"));
        FileUtils.moveFileToDirectory(new File(""),new File(""),true);
        FileUtils.deleteDirectory(new File(""));
    }
}

上一篇下一篇

猜你喜欢

热点阅读