电竞比分网-中国电竞赛事及体育赛事平台

分享

java對(duì)本地日志文件的讀寫

 aaie_ 2016-01-13
package mytools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 這是一個(gè)與日志讀寫有關(guān)的類,定義了一些通用的方法
 * @author Devon
 *
 */
public class LogsReaderWriter {
     
    /**
     *
     * @param filePath      文件路徑的字符串表示形式
     * @param KeyWords      查找包含某個(gè)關(guān)鍵字的信息:非null為帶關(guān)鍵字查詢;null為全文顯示
     * @return      當(dāng)文件存在時(shí),返回字符串;當(dāng)文件不存在時(shí),返回null
     */
    public static String readFromFile(String filePath, String KeyWords){
        StringBuffer stringBuffer = null;
        File file = new File(filePath);
        if(file.exists()){
            stringBuffer = new StringBuffer();
            FileReader fileReader = null;
            BufferedReader bufferedReader = null;
            String temp = "";
            try {
                fileReader = new FileReader(file);
                bufferedReader = new BufferedReader(fileReader);
                while((temp = bufferedReader.readLine()) != null){
                    if(KeyWords ==null){
                        stringBuffer.append(temp + "\n");
                    }else{
                        if(temp.contains(KeyWords)){
                            stringBuffer.append(temp + "\n");
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                //e.printStackTrace();
            } catch (IOException e) {
                //e.printStackTrace();
            }finally{
                try {
                    fileReader.close();
                } catch (IOException e) {
                    //e.printStackTrace();
                }
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    //e.printStackTrace();
                }
            }
        }
        if(stringBuffer == null){
            return null;
        }else{
            return stringBuffer.toString();
        }
         
         
    }
     
    /**
     * 將指定字符串寫入文件。如果給定的文件路徑不存在,將新建文件后寫入。
     * @param log       要寫入文件的字符串
     * @param filePath      文件路徑的字符串表示形式,目錄的層次分隔可以是“/”也可以是“\\”
     * @param isAppend      true:追加到文件的末尾;false:以覆蓋原文件的方式寫入
     */        
      
    public static boolean writeIntoFile(String log, String filePath, boolean isAppend){
        boolean isSuccess = true;
        //如有則將"\\"轉(zhuǎn)為"/",沒(méi)有則不產(chǎn)生任何變化
        String filePathTurn = filePath.replaceAll("\\\\", "/");
        //先過(guò)濾掉文件名
        int index = filePath.lastIndexOf("/");
        String dir = filePath.substring(0, index);
        //創(chuàng)建除文件的路徑
        File fileDir = new File(dir);
        fileDir.mkdirs();
        //再創(chuàng)建路徑下的文件
        File file = null;
        try {
            file = new File(filePath);
            file.createNewFile();
        } catch (IOException e) {
            isSuccess = false;
            //e.printStackTrace();
        }
        //將logs寫入文件
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, isAppend);
            fileWriter.write(log);
            fileWriter.flush();
        } catch (IOException e) {
            isSuccess = false;
            //e.printStackTrace();
        } finally{
            try {
                fileWriter.close();
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }
         
        return isSuccess;
    }
    /**
     * 創(chuàng)建文件,如果該文件已存在將不再創(chuàng)建(即不起任何作用)
     * @param filePath       要?jiǎng)?chuàng)建文件的路徑的字符串表示形式,目錄的層次分隔可以是“/”也可以是“\\”
     * @return      創(chuàng)建成功將返回true;創(chuàng)建不成功則返回false
     */
    public static boolean createNewFile(String filePath){
        boolean isSuccess = true;
        //如有則將"\\"轉(zhuǎn)為"/",沒(méi)有則不產(chǎn)生任何變化
        String filePathTurn = filePath.replaceAll("\\\\", "/");
        //先過(guò)濾掉文件名
        int index = filePathTurn.lastIndexOf("/");
        String dir = filePathTurn.substring(0, index);
        //再創(chuàng)建文件夾
        File fileDir = new File(dir);
        isSuccess = fileDir.mkdirs();
        //創(chuàng)建文件
        File file = new File(filePathTurn);
        try {
            isSuccess = file.createNewFile();
        } catch (IOException e) {
            isSuccess = false;
            //e.printStackTrace();
        }
        return isSuccess;
    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多