commonsIO——java

1 commons-IO

1.1 FileUtils

  1. readFileToString(File file); 读取文件内容,返回一个String

  2. writeStringToFile(File file, String content); 将字符串写入file中

  3. copyFile(File srcFile, FIle destFile); 文件复制

  4. 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(""));
    }
}

你可能感兴趣的:(java)