6

小项目 GIT生成公司EXCEL周报(1)创建读取配置文件

 2 years ago
source link: https://blog.51cto.com/yuniangniang/5453823
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

 周报基础信息配置

ConfigData.java

这个类用于获取配置文件的基本信息,内容如注释所示

小项目 GIT生成公司EXCEL周报(1)创建读取配置文件_配置文件_02

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class ConfigData implements Serializable {
private static final long serialVersionUID = 1L;
private List<String> projectPaths;//项目目录
private List<String> projectNames;//中文名称
private String outputPath;//生成周报存放位置
private String author;//周报的作者
private String career;//职业
private Date startDate;//抓取开始时间,默认本周一
private Date endDate;//抓取结束时间,默认今天
private boolean useEmail;//是否使用 Email 发送周报
private String emailSender;//邮箱发送地址 [email protected]
private String emailPassword;//邮箱发送者密码
private String emailReceiver;//邮箱接收地址 [email protected]

public void writeObject(OutputStream out) throws IOException {
Properties properties = new Properties();
properties.put("projectPaths",join(projectPaths));
properties.put("projectNames",join(projectNames));
properties.put("outputPath",outputPath);
properties.put("author",author);
properties.put("career",career);
properties.put("startDate",toString(startDate));
properties.put("endDate",toString(startDate));
properties.put("useEmail",useEmail?1:0);
if(useEmail){
properties.put("emailSender",emailSender);
properties.put("emailPassword",emailPassword);
properties.put("emailReceiver",emailReceiver);
}
properties.store(out,"写入");

}

public void readObject(InputStream in)
throws IOException, ClassNotFoundException{
Properties properties = new Properties();
properties.load(in);

projectPaths = decode(Arrays.stream(properties.getProperty("projectPaths").split(",")).collect(Collectors.toList()));
projectNames = decode(Arrays.stream(properties.getProperty("projectNames").split(",")).collect(Collectors.toList()));

outputPath = decode(properties.getProperty("outputPath"));
author = decode(properties.getProperty("author"));
career = decode(properties.getProperty("career"));
startDate = toDate(properties.getProperty("startDate"));
endDate = toDate(properties.getProperty("endDate"));
useEmail = "1".equals(properties.getProperty("useEmail"));
if(useEmail){
emailSender = decode(properties.getProperty("emailSender"));
emailPassword = decode(properties.getProperty("emailPassword"));
emailReceiver = decode(properties.getProperty("emailReceiver"));
}

if(startDate == null){
startDate = getDefaultStart();
}

if(endDate == null){
endDate = new Date();
}

}

private List<String> decode(List<String> list){
if(list == null) return list;
int n = list.size();
for(int i =0; i < n; i++){
//编码不行就试试 StandardCharsets.UTF_8
list.set(i,new String(list.get(i).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
}
return list;
}

private String decode(String str){
//编码不行就试试 StandardCharsets.UTF_8
return new String(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}

private static Date getDefaultStart() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
Calendar cld = Calendar.getInstance(Locale.CHINA);
cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
cld.setTimeInMillis(System.currentTimeMillis());//当前时间

cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//周一
return cld.getTime();
}


public String toString(Date d){
SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyy-MM-dd");
return sdfFormat.format(d);
}

public Date toDate(String str){
SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyy-MM-dd");
// 严格模式
sdfFormat.setLenient(false);
try {
return sdfFormat.parse(str);
} catch (ParseException e) {
return null;
}
}

private String join(List<String> strs){
StringBuilder sb = new StringBuilder();
for(String str:strs){
sb.append(str);
sb.append(",");
}
if(sb.length()>0) sb.deleteCharAt(sb.length()-1);
return sb.toString();
}


public List<String> getProjectPaths() {
return projectPaths;
}

public void setProjectPaths(List<String> projectPaths) {
this.projectPaths = projectPaths;
}

public List<String> getProjectNames() {
return projectNames;
}

public void setProjectNames(List<String> projectNames) {
this.projectNames = projectNames;
}

public String getOutputPath() {
return outputPath;
}

public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getCareer() {
return career;
}

public void setCareer(String career) {
this.career = career;
}

public Date getStartDate() {
return startDate;
}

public void setStartDate(Date startDate) {
this.startDate = startDate;
}

public Date getEndDate() {
return endDate;
}

public void setEndDate(Date endDate) {
this.endDate = endDate;
}

public boolean isUseEmail() {
return useEmail;
}

public void setUseEmail(boolean useEmail) {
this.useEmail = useEmail;
}

public String getEmailSender() {
return emailSender;
}

public void setEmailSender(String emailSender) {
this.emailSender = emailSender;
}

public String getEmailPassword() {
return emailPassword;
}

public void setEmailPassword(String emailPassword) {
this.emailPassword = emailPassword;
}

public String getEmailReceiver() {
return emailReceiver;
}

public void setEmailReceiver(String emailReceiver) {
this.emailReceiver = emailReceiver;
}

@Override
public String toString() {

return "[" +
"projectPaths="+join(projectPaths)+"\n"+
"projectNames="+join(projectNames)+"\n"+
"outputPath="+outputPath+"\n"+
"author="+author+"\n"+
"career="+career+"\n"+
"startDate="+toString(startDate)+"\n"+
"endDate="+toString(endDate)+"\n"+
"useEmail="+useEmail+"\n"+
"emailSender="+emailSender+"\n"+
"emailPassword="+emailPassword+"\n"+
"emailReceiver="+emailReceiver+"\n"+
"]";
}
}


Recommend

  • 52

  • 29

    Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口。这三种注解可以配合着@PropertySource来使用,@PropertySource主要是用来指定具体的配置文件。 @PropertySource解析

  • 23
    • studygolang.com 3 years ago
    • Cache

    python读取yaml配置文件

    什么是yaml 1、yaml [ˈjæməl]: Yet Another Markup Language :另一种标记语言。yaml 是专门用来写配置文件的语言,非常简洁和强大, 2、支持多种语言:python、js、golang、java、c、c++ 3、yaml语法...

  • 12

    如何读取yaml,json,ini等配置文件【Golang 入门系列九】 雪域迷影 · 大约8小时之前 · 37 次点击 · 预计阅读时间 3 分钟 · 不到1分钟之前 开始浏览    ...

  • 8

    读取配置文件和自定义配置文件(python实现) 用python读取配置文件比较方便,比如下面一个配置文件: 0.ini文件:--------...

  • 2
    • www.lujun9972.win 3 years ago
    • Cache

    如何找出程序读取的配置文件

    如何找出程序读取的配置文件 解决方案一:查看manual手册 涉及到的命令: man 本部分以archlinux系统为例 man手册中有一个 FILES 章节专门用来提供应用相关文件的说明...

  • 6
    • www.zhyea.com 3 years ago
    • Cache

    GoLang读取yaml配置文件

    最近这段时间在用GoLang写项目。其中如何读取配置文件一度是让我比较头疼的事情,好在最后都解决了,这里简单记录一下GoLang读取配置文件的方式。 因为写了多年java springboot项目的缘故,在多种配置方式中独独比较偏爱yaml的格式。因此为新项目选择配...

  • 8

    V2EX  ›  程序员 springboot 中如何读取配置文件中的动态名称的参数   gibber · 1 天前 · 595...

  • 6
    • studygolang.com 2 years ago
    • Cache

    golang 配置文件读取与写入

    配置文件的读取与解析 config.json 没有配置则读取默认配置 结构体读取,初学者demo,有啥问题可以留言交流 package main import ( "encoding/json" "errors" "fmt" "io/ioutil" "os" "reflect"...

  • 3

    如何安装nginx,并根据不同的项目读取不同的配置文件 原创 公号运维家 2022-03-03 09:55:01...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK