国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

commons io文件操作示例分享

2019-11-26 15:39:30
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

package com.pzq.io;
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, 0, 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;
  }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 措美县| 龙江县| 永和县| 东乌珠穆沁旗| 南岸区| 山东省| 大洼县| 容城县| 金阳县| 海安县| 合作市| 达州市| 加查县| 和政县| 攀枝花市| 泽普县| 巴中市| 崇明县| 搜索| 福贡县| 长丰县| 九台市| 铁力市| 汕尾市| 湟中县| 福清市| 屏山县| 东光县| 广丰县| 十堰市| 炉霍县| 保康县| 洛阳市| 登封市| 汝阳县| 林周县| 康马县| 贺兰县| 大石桥市| 赣榆县| 康平县|