package com.casic.swing.utils; import com.alibaba.fastjson.JSON; import com.casic.swing.bean.FrequencyBean; import java.io.*; import java.util.Calendar; /** * @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(); } } //写入配置文件 String configData = getConfigData(); if ("".equals(configData) || configData.contains("localhost")) { //用于区分是否是第一次启动 saveConfig(); } System.out.println("后台配置路径 ===> " + configFile.getAbsolutePath()); } private static void saveConfig() { try { write(new FileWriter(configFile), "http://localhost:11410"); } catch (IOException e) { e.printStackTrace(); } } public static void saveLog(String data) { try { write(new FileWriter(logFile), data); } catch (IOException e) { e.printStackTrace(); } } private static void write(FileWriter writer, String data) throws IOException { BufferedWriter out = new BufferedWriter(writer); out.write(data); out.flush(); out.close(); } public static String getNtpUpdateTime() { try { InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(logFile)); String s = loadString(inputStreamReader); if ("".equals(s)) { return ""; } /** * 5 Jan 11:20:24 ntpdate[807910]: adjust time server 114.118.7.161 offset -0.004064 sec * 5 Jan 11:24:06 ntpdate[824403]: no server suitable for synchronization found * */ String ntpDate = s.split("ntpdate")[0]; String[] split = ntpDate.split(" "); Calendar date = Calendar.getInstance(); String year = String.valueOf(date.get(Calendar.YEAR)); return year + "-" + monthConvert(split[1]) + "-" + dayConvert(split[0]) + " " + split[2]; } catch (IOException e) { e.printStackTrace(); } return ""; } private static String monthConvert(String engMonth) { switch (engMonth) { case "Jan": return "01"; case "Feb": return "02"; case "Mar": return "03"; case "Apr": return "04"; case "May": return "05"; case "Jun": return "06"; case "Jul": return "07"; case "Aug": return "08"; case "Sept": return "09"; case "Oct": return "10"; case "Nov": return "11"; case "Dec": return "12"; default: return "00"; } } private static String dayConvert(String day) { if (Integer.parseInt(day) < 10) { return "0" + day; } else { return day; } } 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"; } } }