审查视图

src/main/java/com/tianbo/util/IO/FileTool.java 4.1 KB
朱兆平 authored
1 2 3 4 5 6 7
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.*;
朱兆平 authored
8
import java.util.ArrayList;
朱兆平 authored
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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){
朱兆平 authored
48 49 50 51
        List<File> files = new ArrayList<File>();
        if (dir.exists() && dir.isDirectory()) {
             files = (List<File>) FileUtils.listFiles(dir, new String[]{"xml"}, false);
        }
朱兆平 authored
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        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();
朱兆平 authored
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        stringBuffer.append(bakDir).append("/").append(Helper.getUUID()).append(".xml");

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

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

    }

    public static void writeFileToBak(String content,String bakDirectory){
        String bakDir = readProperties("bakDirectory");
        if (bakDirectory!=null && bakDirectory.length()>0){
            bakDir = bakDirectory;
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(bakDir).append("/").append(Helper.getUUID()).append(".xml");
朱兆平 authored
88 89 90 91 92 93 94 95 96 97 98

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

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

    }
朱兆平 authored
99 100 101 102 103
    /**
     * 读取根目录下的config文件夹下的配置文件的节点信息,返回节点的值
     * @param key 配置文件节点
     * @return 返回节点的值
     */
朱兆平 authored
104 105 106 107
    public static String readProperties(String key){
        Properties properties = new Properties();
        String value = "";
        try{
朱兆平 authored
108
            //读取根目录下的config文件夹下的配置文件
朱兆平 authored
109 110 111 112 113 114 115 116 117 118 119 120 121
            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;

    }


}