加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Commons-io文件操作

发布时间:2020-05-24 20:39:41 所属栏目:Java 来源:互联网
导读:Commons-io文件操作

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
  
import org.apache.commons.io.FileUtils;
  
  
/**
 * 文件操作工具类
 * @version 1.0 2013/07/16
 *
 */
public class FileUtil {
      
    /**  
     * 复制文件或者目录,复制前后文件完全一样。  
     * @param resFilePath   源文件路径  
     * @param distFolder    目标文件夹  
     * @IOException         当操作发生异常时抛出  
     */
    public static void copyFile(String resFilePath,String distFolder)  
            throws IOException {  
        File resFile = new File(resFilePath);  
        File distFile = new File(distFolder);  
        if (resFile.isDirectory()) { // 目录时  
            FileUtils.copyDirectoryToDirectory(resFile,distFile);  
        } else if (resFile.isFile()) { // 文件时  
            // FileUtils.copyFileToDirectory(resFile,distFile,true);  
            FileUtils.copyFileToDirectory(resFile,distFile);  
        }  
    }  
      
      
    /**  
     * 删除一个文件或者目录  
     * @param targetPath     文件或者目录路径  
     * @IOException 当操作发生异常时抛出  
     */
    public static void deleteFile(String targetPath) throws IOException {  
        File targetFile = new File(targetPath);  
        if (targetFile.isDirectory()) {  
            FileUtils.deleteDirectory(targetFile);  
        } else if (targetFile.isFile()) {  
            targetFile.delete();  
        }  
    }  
   
    /**  
     * 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)  
     *   
     * @param res         原字符串  
     * @param filePath    文件路径  
     * @return 成功标记  
     * @throws IOException 
     */
    public static boolean string2File(String res,String filePath) throws IOException {  
        boolean flag = true;  
        BufferedReader bufferedReader = null;  
        BufferedWriter bufferedWriter = null;  
        try {  
            File distFile = new File(filePath);  
            if (!distFile.getParentFile().exists()) {// 不存在时创建  
                distFile.getParentFile().mkdirs();  
            }  
            bufferedReader = new BufferedReader(new StringReader(res));  
            bufferedWriter = new BufferedWriter(new FileWriter(distFile));  
            char buf[] = new char[1024]; // 字符缓冲区  
            int len;  
            while ((len = bufferedReader.read(buf)) != -1) {  
                bufferedWriter.write(buf,len);  
            }  
            bufferedWriter.flush();  
            bufferedReader.close();  
            bufferedWriter.close();  
        } catch (IOException e) {  
            flag = false;  
            throw e;
        }  
        return flag;  
    }  
      
    /**  
     * 取得指定文件内容 
     *   
     * @param res         原字符串  
     * @param filePath    文件路径  
     * @return 成功标记  
     * @throws IOException 
     */
    public static List<String> getContentFromFile(String filePath) throws IOException {  
        List<String> lists = null;
        try {  
            if(!(new File(filePath).exists())){
                return new ArrayList<String>();
            }
            lists = FileUtils.readLines(new File(filePath),Charset.defaultCharset());
        } catch (IOException e) {  
             throw e;
        }  
        return lists;  
    }  
      
    /**
     * 给指定文件追加内容
     * @param filePath
     * @param contents
     */
    public static void addContent(String filePath,List<String> contents) throws IOException { 
        try {
            FileUtils.writeLines(new File(filePath),contents);
        } catch (IOException e) {
             throw e;
        }
    }
      
  
}


以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读