中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Commons-io文件操作

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
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;
  
  
/**
 * 文件操作工具類(lèi)
 * @version 1.0 2013/07/16
 *
 */
public class FileUtil {
      
    /**  
     * 復(fù)制文件或者目錄,復(fù)制前后文件完全一樣。  
     * @param resFilePath   源文件路徑  
     * @param distFolder    目標(biāo)文件夾  
     * @IOException         當(dāng)操作發(fā)生異常時(shí)拋出  
     */
    public static void copyFile(String resFilePath, String distFolder)  
            throws IOException {  
        File resFile = new File(resFilePath);  
        File distFile = new File(distFolder);  
        if (resFile.isDirectory()) { // 目錄時(shí)  
            FileUtils.copyDirectoryToDirectory(resFile, distFile);  
        } else if (resFile.isFile()) { // 文件時(shí)  
            // FileUtils.copyFileToDirectory(resFile, distFile, true);  
            FileUtils.copyFileToDirectory(resFile, distFile);  
        }  
    }  
      
      
    /**  
     * 刪除一個(gè)文件或者目錄  
     * @param targetPath     文件或者目錄路徑  
     * @IOException 當(dāng)操作發(fā)生異常時(shí)拋出  
     */
    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();  
        }  
    }  
   
    /**  
     * 將字符串寫(xiě)入指定文件(當(dāng)指定的父路徑中文件夾不存在時(shí),會(huì)最大限度去創(chuàng)建,以保證保存成功!)  
     *   
     * @param res         原字符串  
     * @param filePath    文件路徑  
     * @return 成功標(biāo)記  
     * @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()) {// 不存在時(shí)創(chuàng)建  
                distFile.getParentFile().mkdirs();  
            }  
            bufferedReader = new BufferedReader(new StringReader(res));  
            bufferedWriter = new BufferedWriter(new FileWriter(distFile));  
            char buf[] = new char[1024]; // 字符緩沖區(qū)  
            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;  
    }  
      
    /**  
     * 取得指定文件內(nèi)容 
     *   
     * @param res         原字符串  
     * @param filePath    文件路徑  
     * @return 成功標(biāo)記  
     * @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;  
    }  
      
    /**
     * 給指定文件追加內(nèi)容
     * @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;
        }
    }
      
  
}


標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:DateUtils.java 日期處理相關(guān)工具類(lèi)

下一篇:hibernate框架BaseDao