Android 通过java bean 将数据写入csv文件
程序员文章站
2024-03-21 08:42:22
...
前几天遇到了将csv文件导入到android应用中的需求,自己运用java反射技术实现了该功能,今天又遇到了将android SQLite数据库中的数据导出到csv文件中并同步到后台服务器中的需求。于是乎,基于上一篇文章Android 读取csv文件并转换为java bean,继续改造代码,让其支持Java Bean导出数据到csv文件中。废话不多说,直接上代码:
改造后的CsvField.java
import java.lang.annotation.*;
/**
* @author [email protected]
* Created by Administrator on 2019/9/23 0023.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CsvField {
/**
* 要将某个java类字段持久化到csv文件里的表头字段名称
* @return 表头字段名称
*/
String name();
/**
* 是否将该java类属性持久化到文件内
* @return 默认true,将属性持久化到文件内。false忽略该字段
*/
boolean isWrite() default true;
}
CsvWriter.java
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
/**
* @author [email protected]
* Created by Administrator on 2019/9/26 1323.
*/
public class CsvWriter {
private static CsvWriter instance = null;
private String SEPARATOR = ",";
private String CHARSET = "UTF-8";
public static CsvWriter getInstance(String separator, String charset) {
instance = getInstance();
instance.CHARSET = charset;
instance.SEPARATOR = separator;
return instance;
}
public static CsvWriter getInstance() {
if (instance == null) {
instance = new CsvWriter();
}
return instance;
}
private CsvWriter() {
}
/**
* 存储csv文件的表头数据和Java Bean Field 的对应关系
*/
private static Map<String, Field> map = new LinkedHashMap<>();
private <T> void writeDataToFile(List<T> dataList, String targetFile) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
// TODO: 2019/9/26 13:12 从集合中获取一个实例对象,根据对象使用反射技术获取所有的 Field
T obj = dataList.get(0);
Class<?> clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
CsvField csvField = field.getAnnotation(CsvField.class);
if (csvField != null) {
String name = csvField.name();
boolean write = csvField.isWrite();
if (write) {
map.put(name, field);
}
}
}
Set<String> csvFieldNames = map.keySet();
List<String> tmp;
OutputStream output;
output = new FileOutputStream(new File(targetFile));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output, CHARSET));
bw.write(String.join(SEPARATOR, csvFieldNames));
bw.newLine();
for (T o : dataList) {
tmp = new ArrayList<>();
for (String csvFieldName : csvFieldNames) {
Field field = map.get(csvFieldName);
String fieldName = field.getName();
String firstUpperCase = getFirstUpperCase(fieldName);
Method method = clazz.getMethod("get" + firstUpperCase);
Object invoke = method.invoke(o);
String value = String.valueOf(invoke);
tmp.add(value);
}
String join = String.join(SEPARATOR, tmp);
bw.write(join);
bw.newLine();
}
bw.flush();
bw.close();
}
/**
* 字符串首字母转大写
*
* @param fieldName 字段名称
* @return 首字母大写的字段名称
*/
private static String getFirstUpperCase(String fieldName) {
char[] chars = fieldName.toCharArray();
String s = String.valueOf(chars[0]).toUpperCase();
chars[0] = s.toCharArray()[0];
return new String(chars);
}
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException {
List<Oil> list = new ArrayList<>();
list.add(new Oil(1, "001", "A油"));
list.add(new Oil(2, "002", "B油"));
CsvWriter.getInstance("^", "UTF-8")
.writeDataToFile(list, "E:\\data\\output.csv");
}
}
Oil.java
import java.io.Serializable;
/**
* @author [email protected]
* Created by Administrator on 2019/9/26 0023.
*/
public class Oil implements Serializable {
public Oil(Integer id, String oilCode, String oilName) {
this.id = id;
this.oilCode = oilCode;
this.oilName = oilName;
}
@CsvField(name = "id")
private Integer id;
@CsvField(name = "oil_code",isWrite = false)
private String oilCode;
@CsvField(name = "oil_name")
private String oilName;
public String getOilCode() {
return oilCode;
}
public void setOilCode(String oilCode) {
this.oilCode = oilCode;
}
public String getOilName() {
return oilName;
}
public void setOilName(String oilName) {
this.oilName = oilName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}