# yn-tools
**Repository Path**: xiao-yang222/yn-tools
## Basic Information
- **Project Name**: yn-tools
- **Description**: web开发简化工具,包括统一返回值,数据校验,统一异常处理等
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: develop
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2024-11-12
- **Last Updated**: 2024-11-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
__您的star是我继续前进的动力,如果喜欢yn-tools请右上角帮忙点个star__
## 概述
yn-tools主要是为了总结工作中简化处理的部分工具类的整合。目前包含以下内容
- 统一异常处理
- 统一返回值处理
- 文本读写
## 使用教程
使用要求:jdk >=1.8
example地址:https://gitee.com/yeningx/yn-tools-example
### 第一步:引入依赖
```
io.gitee.yeningx
yn-tools
1.3
implementation 'io.gitee.yeningx:yn-tools:1.3'
```
### 第二步:配置启用
```java
// 通过注解EnableYnTools启用
@SpringBootApplication
@EnableYnTools
public class CoreApplication {
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
```
### 文本处理
```java
@Test
@DisplayName("生成文本文件")
public void test1() throws Exception {
String basePath = "/Users/johnny/Desktop";
// 临时存储数据行字段数据
List tmpList = new ArrayList<>();
StringBuilder buffer = new StringBuilder();
// 假设设计有100000行
for (int i = 0; i < 100000; i++) {
// 按照指定字段和类型生成文本
String[] cols = getConfig().get("table_cols").split(",");
String[] types = getConfig().get("table_types").split(",");
for (int j = 0; j < cols.length; j++) {
// 将字段数据转换为String,写入List,后面拼接到buffer中
if (types[j].equalsIgnoreCase(TextConst.DataType.LocalDateTime))
tmpList.add(TextWriter.formatProperty2Str(types[j], LocalDateTime.now()));
else if (types[j].equalsIgnoreCase(TextConst.DataType.Decimal))
tmpList.add(TextWriter.formatProperty2Str(types[j], new BigDecimal(32)));
else {
tmpList.add(TextWriter.formatProperty2Str(types[j], cols[j] + "中文_" + i));
}
}
buffer.append(String.join(TextConst.LINE_DATA_SEPARATOR, tmpList)).append("\n");
tmpList.clear();
if (i % 1000 == 0 && i > 0) {
TextWriter.writeText(buffer, basePath + "/" + getConfig().get("table") + ".txt", getConfig().get("table_cols") + "\n" + getConfig().get("table_types"), true);
}
}
TextWriter.writeText(buffer, basePath + "/" + getConfig().get("table") + ".txt", getConfig().get("table_cols") + "\n" + getConfig().get("table_types"), true);
}
@Test
@DisplayName("读取文本文件")
public void test2() throws Exception {
String filePath = "/Users/johnny/Desktop/" + getConfig().get("table") + ".txt";
TextReader reader = new TextReader(filePath);
// 从文本中读取字段和类型
String[] headerArr = reader.next().split(",");
String[] typeArr = reader.next().split(",");
while (reader.hasNext()) {
// 按行读取数据
String line = reader.next();
line = line.replace("\n", "");
String[] dataArr = line.split(TextConst.LINE_DATA_SEPARATOR);
Map mp = new HashMap<>();
for (int i = 0; i < headerArr.length; i++) {
// 解析行数据到map中
mp.put(headerArr[i], TextReader.formatPropertyByType(typeArr[i], dataArr[i]));
}
//按行+字段将数据打印到控制台
for (Map.Entry entry : mp.entrySet()) {
System.out.print(entry.getKey() + ":" + entry.getValue() + "\t\t");
}
System.out.println();
}
}
/**
* 获取对应配置信息
* type:可选值:boolean/int/long/float/double/date/localdate/localdatetime/string
*/
private Map getConfig() {
Map mp = new HashMap<>();
mp.put("table_cols", "col1,col2,col3,col4,col5");
mp.put("table_types", "String,String,String,decimal,localdatetime");
mp.put("table", "table1");
return mp;
}
```
开源不易,支持就请赞助yn-tools,请我喝一杯咖啡吧