摘要:基于注解生成加簽驗簽。是否寫入頭,建議第一次寫入指定,避免中文亂碼指定文件編碼默認不進行字段排序無待寫入的文件列表方法默認值說明必填創(chuàng)建實例,并且指定待讀取文件路徑。
csv
基于 java 注解生成加簽驗簽 csv。
開源地址: github csv
)
以前覺得 csv 文件的多寫非常簡單,就懶得封裝。
最近一個月寫了兩次 csv 文件相關(guān)的東西,發(fā)現(xiàn)要處理的細節(jié)還是有的,還浪費比較多的時間。
比如:
UTF-8 中文編碼使用 excel 打開亂碼,因為缺少 BOM 頭。
不同類型字段轉(zhuǎn)化為字符串,順序的指定,head 頭的指定,如果手寫都會很繁瑣。
讀取的時候最后 , 后無元素,split 會缺失等。
為了解決上述問題,此框架應(yīng)運而生。
特性Fluent 流式寫法
基于 java 注解
字段類型轉(zhuǎn)換的靈活支持,內(nèi)置 8 大基本類型以及 String 類型轉(zhuǎn)換
快速開始 環(huán)境jdk7+
maven 3.x
maven 引入示例代碼com.github.houbb csv 0.0.2
User.java
演示基本類型的轉(zhuǎn)換
public class User { private String name; private int age; private float score; private double money; private boolean sex; private short level; private long id; private char status; private byte coin; //Getter & Setter & toString() }
對象列表構(gòu)建
/** * 構(gòu)建通用測試列表 * @return 列表 */ private List寫入buildCommonList() { User user = new User(); short s = 4; byte b = 1; user.age(10) .name("你好") .id(1L) .score(60) .coin(b) .level(s) .money(200) .sex(true) .status("Y"); return Arrays.asList(user); }
測試代碼
public void commonTest() { final String path = "src est esourcescommon.csv"; CsvWriteBs.newInstance(path) .write(buildCommonList()); }
文件生成
name,age,score,money,sex,level,id,status,coin 你好,10,60.0,200.0,true,4,1,Y,1讀取
public void commonTest() { final String path = "src est esourcescommon.csv"; ListuserList = CsvReadBs.newInstance(path) .read(User.class); System.out.println(userList); }
日志信息
[User{name="你好", age=10, score=60.0, money=200.0, sex=true, level=4, id=1, status=Y, coin=1}]CSV 引導類
為了用戶使用的便利性,和后期拓展的靈活性。
引導類CSV 有兩個引導類:
名稱 | 作用 |
---|---|
CsvWriteBs | csv 文件寫入引導類 |
CsvReadBs | csv 文件讀取引導類 |
方法 | 默認值 | 說明 |
---|---|---|
newInstance(final String path) | 必填 | 創(chuàng)建實例,并且指定待寫入文件路徑。 |
writeBom(boolean writeBom) | true | 是否寫入 UTF8 BOM 頭,建議第一次寫入指定,避免中文亂碼 |
charset(String charset) | UTF-8 | 指定文件編碼 |
sort(ISort sort) | NoSort | 默認不進行字段排序 |
write(List |
無 | 待寫入的文件列表 |
方法 | 默認值 | 說明 |
---|---|---|
newInstance(final String path) | 必填 | 創(chuàng)建實例,并且指定待讀取文件路徑。 |
charset(String charset) | UTF-8 | 指定文件編碼 |
sort(ISort sort) | NoSort | 默認不進行字段排序 |
startIndex(int startIndex) | 1 | 文件的第二行,默認第一行是 head |
endIndex(int endIndex) | 文件的最后一行 |
用于待處理對象的字段上。
/** * 字段顯示名稱 * 1. 默認使用 field.name * @return 顯示名稱 */ String label() default ""; /** * 讀取是否需要 * @return 是 */ boolean readRequire() default true; /** * 寫入是否需要 * @return 是 */ boolean writeRequire() default true; /** * 讀取轉(zhuǎn)換 * @return 處理實現(xiàn)類 */ Class extends IReadConverter> readConverter() default CommonReadConverter.class; /** * 寫入轉(zhuǎn)換 * @return 處理實現(xiàn)類 */ Class extends IWriteConverter> writeConverter() default StringWriteConverter.class;屬性概覽表
屬性 | 默認值 | 說明 |
---|---|---|
label | 字段名稱 | 用于 csv 頭生成 |
readRequire | true | 是否需要從 csv 文件讀取 |
writeRequire | true | 當前字段是否需要寫入 csv 文件 |
readConverter | CommonReadConverter | 將 csv 中的字符串轉(zhuǎn)化為當前字段類型,支持 8 大基本類型+String |
writeConverter | StringWriteConverter | 直接調(diào)用當前字段值 toString() 方法,null 直接為空字符串 |
其中 readConverter/writeConverter 支持用戶自定義
注解使用代碼示例 對象定義public class UserAnnotation { @Csv(label = "名稱") private String name; @Csv(label = "密碼", readRequire = false, writeRequire = false) private String password; @Csv(label = "生日", readConverter = ReadDateConvert.class, writeConverter = WriteDateConvert.class) private Date birthday; //Getter & Setter & toString() }ReadDateConvert/WriteDateConvert
使我們自定義的針對 Date 的轉(zhuǎn)換實現(xiàn)。
Write
public class WriteDateConvert implements IWriteConverter{ @Override public String convert(Date value) { DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); return dateFormat.format(value); } }
ReadDateConvert
public class ReadDateConvert implements IReadConverter寫入文件{ @Override public Date convert(String value, Class fieldType) { try { DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); return dateFormat.parse(value); } catch (ParseException e) { throw new RuntimeException(e); } } }
public void annotationTest() { final String path = "src est esourcesannotation.csv"; CsvWriteBs.newInstance(path) .write(buildAnnotationList()); }
其中列表構(gòu)建:
/** * 構(gòu)建基于注解的測試列表 * @return 列表 */ private ListbuildAnnotationList() { UserAnnotation user = new UserAnnotation(); user.name("你好") .password("123") .birthday(new Date()); return Arrays.asList(user); }
生成文件內(nèi)容
名稱,生日 你好,20190603讀取文件測試
public void annotationTest() { final String path = "src est esourcesannotation.csv"; ListuserList = CsvReadBs.newInstance(path) .read(UserAnnotation.class); System.out.println(userList); }
日志信息
[UserAnnotation{name="你好", password="null", birthday=Mon Jun 03 00:00:00 CST 2019}]
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/74784.html
摘要:創(chuàng)作原由以前覺得文件的讀寫非常簡單,就懶得封裝。為了解決上述問題,此框架應(yīng)運而生。寫入文件其中列表構(gòu)建構(gòu)建基于注解的測試列表列表你好生成文件內(nèi)容名稱生日你好讀取文件測試日志信息你好集合類有時候?qū)ο笾袝瑪?shù)組等常見集合。 CSV 基于 java 注解的 csv 讀寫框架。 相關(guān)框架 Apache commons-csv super-csv 簡單看了下,這兩個框架提供的特性都非常的基礎(chǔ)。...
摘要:集合類有時候?qū)ο笾袝瑪?shù)組等常見集合。為了存儲的便利性,默認提供集合的相關(guān)支持。特性和普通字段保持一致,如果指定注解轉(zhuǎn)換,則以注解為準。集合使用進行分隔,其中的分隔,用到了。在使用時要注意,不要包含上述的符號,否則會出現(xiàn)解析錯亂。 集合類 有時候?qū)ο笾袝瑪?shù)組、Map、Collection 等常見集合。 為了存儲的便利性,默認提供集合的相關(guān)支持。 特性和普通字段保持一致,如果指定注...
摘要:本次分享將介紹如何在中使用庫實現(xiàn)數(shù)據(jù)庫的讀寫。提供了工具包及對象關(guān)系映射工具,使用許可證發(fā)行。模塊實現(xiàn)了與不同數(shù)據(jù)庫的連接,而模塊則使得能夠操作數(shù)據(jù)庫。 ??本次分享將介紹如何在Python中使用Pandas庫實現(xiàn)MySQL數(shù)據(jù)庫的讀寫。首先我們需要了解點ORM方面的知識。 ORM技術(shù) ??對象關(guān)系映射技術(shù),即ORM(Object-Relational Mapping)技術(shù),指的是把關(guān)...
閱讀 2846·2021-09-10 10:50
閱讀 2201·2019-08-29 16:06
閱讀 3205·2019-08-29 11:02
閱讀 1104·2019-08-26 14:04
閱讀 2821·2019-08-26 13:24
閱讀 2314·2019-08-26 12:16
閱讀 561·2019-08-26 10:29
閱讀 3106·2019-08-23 18:33