springboot对resource目录下配置文件读取和修改,生成流水号

 主方法为getTeacherFlowCode(),内mian主函数,只需文件放在项目resourc下。

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

 springboot对resource目录下配置文件读取和修改,生成流水号_第1张图片

参考: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 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 mapNew= new HashMap();
        	mapNew.put(FLOWCODE,"1");//因为0当前已被用
        	mapNew.put(TODAY,todayStr);
        	updateProperties(FILENAME,mapNew);
        	flowCodeInt=1;
        }else {
        	Map mapNew= new HashMap();
        	mapNew.put(FLOWCODE,""+flowCodeInt);//因为0当前已被用
        	mapNew.put(TODAY,todayStr);
        	updateProperties(FILENAME,mapNew);
        }
		return "T" + todayStr+ String.format("%03d", flowCodeInt);
	}

	public static Map getProfileByClassLoader(String fileName) {
		// 通过ClassLoader获取到文件输入流对象
		InputStream in = Profile.class.getClassLoader().getResourceAsStream(fileName);
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		Properties props = new Properties();
		Map profileMap = new HashMap();
		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 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());
		}	
	}

}

 

你可能感兴趣的:(java,流水号,springboot,读写配置)