commons-io 之FileUtils学习

package com.fanfan.commons.io;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.junit.Test;

public class FileUtilsTest {
private File srcFile;
private File destFile;
private String rootPath = “E:\1A”;
private String srcPath = “E:\1A\src\”;
private String destPath = “E:\1A\dest\”;
private String dirPath = “E:\1A\dir\”;

/**
 * 需求 1.判断是否存在文件
 */
@Test
public void testCopyFile() {
}

/**
 * 拷贝一个文件夹的内容
 * 
 * @throws IOException
 */
@Test
public void copyFileContent() throws IOException {
    srcFile = new File(srcPath + "test.txt");
    isExistFolderAndFile(srcFile);
    destFile = new File(destPath + "test2.txt");
    isExistFolderAndFile(destFile);
    FileUtils.copyFile(srcFile, destFile);
}

/**
 * 拷贝一个文件夹下的所有文件夹和文件
 * 
 * @throws IOException
 */
@Test
public void copyDirectory() throws IOException {
    srcFile = new File(dirPath);
    isExistFolderAndFile(srcFile);
    destFile = new File(destPath);
    isExistFolderAndFile(destFile);
    FileUtils.copyDirectory(srcFile, destFile);

// FileUtils.copyDirectory(srcFile, destFile, false);// 不保存文件修改的时间
}

/**
 * 拷贝一个文件夹下的所有文件夹和文件 并且过滤指定类型
 * 
 * @throws IOException
 */
@Test
public void copyDirectoryFiterType() throws IOException {
    srcFile = new File(dirPath);
    isExistFolderAndFile(srcFile);
    destFile = new File(destPath);
    isExistFolderAndFile(destFile);
    FileFilter fileFiter = new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory())
                return true;
            else {
                String name = file.getName();
                if (name.endsWith(".zip") || name.endsWith(".txt"))
                    return true;
                else
                    return false;
            }
        }
    };
    FileUtils.copyDirectory(srcFile, destFile, fileFiter); // 复制后会覆盖原先的数据

}

/**
 * 把服务器上文件下载到本地
 * 
 * @throws IOException
 */
@Test
public void copyURLToFile() throws IOException {
    String url = "http://imgsrc.baidu.com/baike/pic/item/7aec54e736d12f2ee289bffe4cc2d5628435689b.jpg";
    destFile = new File(destPath + "copyURLToFile");
    isExistFolderAndFile(destFile);
    FileUtils.copyURLToFile(new URL(url), destFile);
}

/**
 * 把字符串写入文件
 * 
 * @throws IOException
 */
@Test
public void writeStringToFile() throws IOException {
    String content = "http://imgsrc.baidu.com/baike/pic/item/7aec54e736d12f2ee289bffe4cc2d5628435689b.jpg";
    destFile = new File(destPath + "writeStringToFile");
    isExistFolderAndFile(destFile);
    FileUtils.writeStringToFile(destFile, content, Charsets.UTF_8, true);
}

/**
 * 把文件读取成字符串
 * 
 * @throws IOException
 */
@Test
public void readFileToString() throws IOException {
    String content = "";
    destFile = new File(destPath + "writeStringToFile");
    isExistFolderAndFile(destFile);
    content = FileUtils.readFileToString(destFile, Charsets.UTF_8);
    System.out.println(content);
}

/**
 * 把byte[]写入文件
 * 
 * @throws IOException
 */
@Test
public void writeByteArrayToFile() throws IOException {
    String content = "http://imgsrc.baidu.com/baike/pic/item/7aec54e736d12f2ee289bffe4cc2d5628435689b.jpg";
    byte[] contentArray = content.getBytes();
    destFile = new File(destPath + "writeByteArrayToFile");
    isExistFolderAndFile(destFile);
    FileUtils.writeByteArrayToFile(destFile, contentArray, true);
}

/**
 * 把文件读取成 byte[]
 * 
 * @throws IOException
 */
@Test
public void readFileToByteArray() throws IOException {
    String content = "";
    byte[] contentArray = content.getBytes();
    destFile = new File(destPath + "writeByteArrayToFile");
    isExistFolderAndFile(destFile);
    contentArray = FileUtils.readFileToByteArray(destFile);
    System.out.println(ArrayUtils.toString(contentArray));
    System.out.println(Arrays.toString(contentArray));
    System.out.println(new String(contentArray, Charsets.UTF_8));
}

/**
 * 把集合写入文件
 * 
 * @throws IOException
 */
@Test
public void writeLines() throws IOException {
    ArrayList<String> list = new ArrayList<>();
    String mes = "哈哈,下班了";
    String mes2 = "回家,回家";
    list.add(mes); // 第一行数据
    list.add(mes2); // 第二行数据
    destFile = new File(destPath + "writeLines");
    isExistFolderAndFile(destFile);
    FileUtils.writeLines(destFile, list, true);

// FileUtils.writeLines(destFile, list, “以。结尾”, true);
}

/**
 * 把文件读取成集合
 * 
 * @throws IOException
 */
@Test
public void readLines() throws IOException {
    List<String> list = new ArrayList<>();
    destFile = new File(destPath + "writeLines");
    isExistFolderAndFile(destFile);
    list = FileUtils.readLines(destFile, Charsets.UTF_8);
    System.out.println(Arrays.toString(list.toArray()));
}

/**
 * 判断文件夹是否存在,不存在就创建
 * 
 * @param file
 * @return
 */
public boolean isExistFolder(File file) {
    File fileParent = file.getParentFile();
    if (!fileParent.exists()) {
        fileParent.mkdirs();
        return false;
    }
    return true;
}

/**
 * 判断文件夹是否存在,不存在就创建
 * 
 * @param file
 * @return
 */
public boolean isExistFolderAndFile(File file) {
    File fileParent = file.getParentFile();
    if (!fileParent.exists()) {
        fileParent.mkdirs();
        return false;
    }
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
    return true;
}

}

猜你喜欢

转载自blog.csdn.net/mengxiangxingdong/article/details/79394127