Newer
Older
CasicTimeGuard / src / main / java / com / casic / swing / utils / StringHelper.java
package com.casic.swing.utils;

import com.alibaba.fastjson.JSON;
import com.casic.swing.bean.FrequencyBean;

import java.io.*;

/**
 * @author a203
 */
public class StringHelper {

    private static File logFile;
    private static File configFile;

    public static void createLogFile() {
        File rootDir = new File(System.getProperty("user.dir") + File.separator + "logs");
        if (!rootDir.exists()) {
            rootDir.mkdir();
        }
        logFile = new File(rootDir + File.separator + "update.log");
        configFile = new File(rootDir + File.separator + "config.txt");
        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("同步记录路径 ===> " + logFile.getAbsolutePath());
        if (!configFile.exists()) {
            try {
                configFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //写入配置文件
        try {
            FileWriter writer = new FileWriter(configFile);
            BufferedWriter out = new BufferedWriter(writer);
            out.write("http://localhost:11410");
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("后台配置路径 ===> " + configFile.getAbsolutePath());
    }

    public static void saveAssertsData(String data) {
        try {
            FileWriter writer = new FileWriter(logFile);
            BufferedWriter out = new BufferedWriter(writer);
            out.write(data);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getLogData() {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(logFile));
            return loadString(inputStreamReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getConfigData() {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(configFile));
            return loadString(inputStreamReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String loadString(InputStreamReader reader) {
        BufferedReader bufferedReader = new BufferedReader(reader);
        StringBuilder data = new StringBuilder();
        String s;
        try {
            while ((s = bufferedReader.readLine()) != null) {
                data.append(s);
            }
            return data.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String parseJson(String json) {
        FrequencyBean bean = JSON.parseObject(json, FrequencyBean.class);
        if (bean.getCode() == 200) {
            FrequencyBean.DataBean data = bean.getData();
            return data.getParaValue();
        } else {
            return "10";
        }
    }
}