项目任务1 创建并读取配置文件

这个项目就是不想写周报的时候偷懒用的,直接拿git记录生成日志用的。当前这部分是要先给个基础的配置数据,后面才好进行其他的。配的就是名称、项目路径、发送邮箱等

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

 周报基础信息配置

ConfigData.java

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

小项目 GIT生成公司EXCEL周报(1)创建读取配置文件_java_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;//邮箱发送地址 XX@XX.com
private String emailPassword;//邮箱发送者密码
private String emailReceiver;//邮箱接收地址 XX@XX.com

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"+
"]";
}
}

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

然后就要写一下配置文件,这个随意填一下就行,如下:

projectPaths=D:/program/mine/weekpaper
projectNames=周报
outputPath=D:/program/mine/weekpaper/out/
author=xx
career=xx
startDate=
endDate=
useEmail=1
emailSender=xx@xx.com
emailPassword=xx
emailReceiver=xx@xx.com

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

 控制台日志配置

我们希望,使用一个单独的类来处理日志信息,用于配置日志颜色。红色代表错误,白色代表信息,如下:

LogUtil.java

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

import java.util.logging.Logger;

public class LogUtil {
private static Logger log = Logger.getGlobal();
static {
log.getParent().getHandlers()[0].setFormatter(new LoggerFormatter());
}

//控制台字体颜色设置:红色 [30m=[37m 对应不同控制台颜色
private static String consoleRed(){
return "\033[31m";
}

//控制台字体颜色设置:白色
private static String consoleWhite(){
return "\033[30m";
}

public static void severe(String msg){
log.severe(consoleRed()+msg);
}

public static void info(String msg){
log.info(consoleWhite()+msg);
}

}

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

LoggerFormatter.java

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

import sun.util.logging.LoggingSupport;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

public class LoggerFormatter extends SimpleFormatter {
private final String format = LoggingSupport.getSimpleFormat();
private final Date dat = new Date();

public synchronized String format(LogRecord var1) {
this.dat.setTime(var1.getMillis());
String var2;
if (var1.getSourceClassName() != null) {
var2 = var1.getSourceClassName();
if (var1.getSourceMethodName() != null) {
var2 = var2 + " " + var1.getSourceMethodName();
}
} else {
var2 = var1.getLoggerName();
}

String var3 = this.formatMessage(var1);
String var4 = "";
if (var1.getThrown() != null) {
StringWriter var5 = new StringWriter();
PrintWriter var6 = new PrintWriter(var5);
var6.println();
var1.getThrown().printStackTrace(var6);
var6.close();
var4 = var5.toString();
}

//从日志中摘取颜色信息,拼接到开头(不设置会导致前面的头部信息(日志名,类名,时间等)颜色不一致)
String color = var3.substring(0, "\033[31m".length());
return String.format(color + format, this.dat, var2, var1.getLoggerName(), var1.getLoggerName().toUpperCase(), var3, var4);
}
}

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

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

 文件处理

  • 获取文件标准路径
  • 加载配置文件

FileUtil.java

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

import com.weekpaper.bean.ConfigData;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileUtil {
/**
* 获取文件标准绝对路径
* @param path
* @return
*/
public static String getFilePath(String path){
try {
return new File(path).getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

/**
* 加载配置文件
* @param configPath
* @return
*/
public static ConfigData loadConfigData(String configPath){
FileInputStream fis = null;
try {
fis = new FileInputStream(configPath);
ConfigData cd = new ConfigData();
cd.readObject(fis);
return cd;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return null;
}
}

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

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

 主程序加载配置文件

根据传参给出的配置文件地址,加载配置文件信息,加载不出来时,进行提示

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

public class WeekPaper {

public static void main(String[] args) {
if(args==null || args.length<1){
LogUtil.severe("找不到配置文件!");
System.exit(0);
}
//1.加载配置文件信息
ConfigData cd = FileUtil.loadConfigData(args[0]);
if(cd == null){
LogUtil.severe("配置内容有误!");
System.exit(0);
}else{
LogUtil.info("获取配置文件"+ FileUtil.getFilePath(args[0])+"成功!信息如下:");
LogUtil.info(cd.toString());
}
}

}

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

配置好配置文件地址:

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

运行结果:

🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍

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

🚗🚓🚕🛺🚙🚌🚐🚎🚑🚒🚚🚛🚜🚘🚔🚖🚍🦽🦼🛹🚲🛴🛵🏍