FileTool.java 3.2 KB
package com.tianbo.util.IO;

import com.tianbo.util.Date.DateUtil;
import com.tianbo.util.Helper;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.List;
import java.util.Properties;


public final class FileTool {
    private final static String errorRootDirectory = "errorLogs";//错误的根目录名
    private final static String xmlRootDirectory = "xmlLog"; //记录已收到的报文目录
    private final static String Cherector = "UTF-8";

    /**
     * 写入文件
     * @param path 二级目录
     * @param content 写入内容
     * @param rightOrwrong 是写入错误记录目录还是记录目录
     */
    public static void writeFile(String path,String content,boolean rightOrwrong){
        StringBuffer stringBuffer = new StringBuffer();

        if (rightOrwrong){
            stringBuffer.append(xmlRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(".log");
        }else {
            stringBuffer.append(errorRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(".log");
        }

        File file = new File(stringBuffer.toString());

        try{
            FileUtils.writeStringToFile(file,content,Cherector);
        }catch (IOException e){
            e.printStackTrace();
        }

    }
    public static String readfile(File file) throws IOException{
        String fileToString = FileUtils.readFileToString(file, "UTF-8");
        return fileToString;
    }

    public static List<File> readDirectoryFiles(File dir){
        List<File> files = (List<File>)FileUtils.listFiles(dir,new String[]{"xml"},false);
        return files;
    }
    public static void writeWaybill(String path,String content,String waybillNo){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(xmlRootDirectory).append("/").append(path).append("/").append(DateUtil.getToday()).append("/").append(waybillNo).append(".log");
        File file = new File(stringBuffer.toString());
        try{
            FileUtils.writeStringToFile(file,content,Cherector);
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    public static void writeFileToBak(String content){
        String bakDir = readProperties("bakDirectory");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(bakDir).append("/").append(Helper.getUUID()).append(".txt");

        File file = new File(stringBuffer.toString());

        try{
            FileUtils.writeStringToFile(file,content,Cherector);
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    public static String readProperties(String key){
        Properties properties = new Properties();
        String value = "";
        try{
            BufferedReader bufferedReader = new BufferedReader(new FileReader("config/config.properties"));
            properties.load(bufferedReader);
            //获取key对应的value值
            value=  properties.getProperty(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  value;

    }


}