springboot對resource目錄下配置文件讀取和修改,生成流水號

 主方法爲getTeacherFlowCode(),內mian主函數,只需文件放在項目resourc下。

#\u6539\u53D8\u6570\u636E
#Thu Apr 18 00:16:02 CST 2019
wf.FLOWCODE=20
wf.TODAY=20190418

 

參考:https://blog.csdn.net/qq_33183022/article/details/84328499#commentBox

package com.gsschool.demo.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.context.annotation.Profile;

public class GetWorkFlowCode {

	private static final String FLOWCODE = "wf.FLOWCODE";//定義數的key
	private static final String TODAY = "wf.TODAY";//定義時間的key
	private static final String FILENAME = "keyValue.properties";//定義文件的名

	public static String getTeacherFlowCode() throws Exception {
		
		Map<String, Object> map= getProfileByClassLoader(FILENAME);
		
		int flowCodeInt=Integer.parseInt(map.get(FLOWCODE).toString());
		flowCodeInt=flowCodeInt+1;
		LocalDate nowday = LocalDate.now();
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyyMMdd");
        String todayStr=nowday.format(formatters);
        String todayStrBefor=map.get(TODAY).toString();
        
        if(!todayStr.equals(todayStrBefor)) {
        	Map<String, String> mapNew= new HashMap<String, String>();
        	mapNew.put(FLOWCODE,"1");//因爲0當前已被用
        	mapNew.put(TODAY,todayStr);
        	updateProperties(FILENAME,mapNew);
        	flowCodeInt=1;
        }else {
        	Map<String, String> mapNew= new HashMap<String, String>();
        	mapNew.put(FLOWCODE,""+flowCodeInt);//因爲0當前已被用
        	mapNew.put(TODAY,todayStr);
        	updateProperties(FILENAME,mapNew);
        }
		return "T" + todayStr+ String.format("%03d", flowCodeInt);
	}

	public static Map<String, Object> getProfileByClassLoader(String fileName) {
		// 通過ClassLoader獲取到文件輸入流對象
		InputStream in = Profile.class.getClassLoader().getResourceAsStream(fileName);
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		Properties props = new Properties();
		Map<String, Object> profileMap = new HashMap<String, Object>();
		try {
			props.load(reader);
			for (Object key : props.keySet()) {
				profileMap.put(key.toString(), props.getProperty(key.toString()));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return profileMap;
	}

	public static void updateProperties(String fileName, Map<String, String> keyValueMap) throws Exception {
		// 獲取文件路徑
		String filePath = Profile.class.getClassLoader().getResource(fileName).toURI().getPath();
		System.out.println("propertiesPath:" + filePath);
		Properties props = new Properties();
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			// 從輸入流中讀取屬性列表(鍵和元素對)
			br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
			props.load(br);
			br.close();
			// 寫入屬性文件
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
			// 清空舊的文件
			// props.clear();
			for (String key : keyValueMap.keySet()) {
				props.setProperty(key, keyValueMap.get(key));
			}
			props.store(bw, "改變數據");
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Visit " + filePath + " for updating " + "" + " value error");
		} finally {
			try {
				br.close();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		for(int i=0;i<10;i++) {
			System.out.println(getTeacherFlowCode());
		}	
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章