审查视图

src/main/java/com/sy/utils/FileTool.java 3.5 KB
zhangFan authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
package com.sy.utils;

import org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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(DateTimeConverter.getFormat())
zhangFan authored
29
                    .append("/").append(UUIDCreate.getUUID()).append(".log");
zhangFan authored
30 31
        }else {
            stringBuffer.append(errorRootDirectory).append("/").append(path).append("/").append(DateTimeConverter.getDay())
zhangFan authored
32
                    .append("/").append(UUIDCreate.getUUID()).append(".log");
zhangFan authored
33 34 35 36 37 38 39 40 41 42 43 44
        }

        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{
zhangFan authored
45
        String fileToString = CharasetCheck.getUTF8String(FileUtils.readFileToString(file, "GB2312"));
zhangFan authored
46 47 48
        return fileToString;
    }
49 50 51 52 53
    public static String readfile(File file,String encode) throws IOException{
        String fileToString = CharasetCheck.getUTF8String(FileUtils.readFileToString(file, encode));
        return fileToString;
    }
zhangFan authored
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    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(DateTimeConverter.getDay()).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){
zhangFan authored
73
        String bakDir = readProperties("receiveDirectory");
zhangFan authored
74
        StringBuffer stringBuffer = new StringBuffer();
zhangFan authored
75
        stringBuffer.append(bakDir).append("/").append(UUIDCreate.getUUID()).append(".txt");
zhangFan authored
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

        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;

    }


}