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

2 篇文章 0 订阅
1 篇文章 0 订阅

 主方法为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());
		}	
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值