/**
* fastjson写入方法
* @param list 数据
* @param jsonWriter 生成JSONWriter对象
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static void writeToFile(List<?> list, JSONWriter jsonWriter) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
for (Object object : list) {
jsonWriter.startObject();
Class<? extends Object> model = object.getClass();
Field[] fields = model.getDeclaredFields();
for(Field field : fields) {
//属性名称
String fieldName = field.getName();
//get方法执行结果
Object getMethodResult = invokeGetMethod(fieldName,model,object);
if(!(getMethodResult instanceof Date)) {
jsonWriter.writeKey(fieldName);
jsonWriter.writeObject(getMethodResult);
} else {
if(getMethodResult != null) {
jsonWriter.writeKey(fieldName);
jsonWriter.writeObject(DateUtils.formatDateTime((Date)getMethodResult));
}
}
}
jsonWriter.endObject();
}
}
/**
* fastjson 读取方法
* @param object 实体类
* @param jsonReader
*InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("D:\\dataBackups\\cangku.dat"),"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
JSONReader jsonReader = new JSONReader(bufferedReader);
*
*
* @return
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws NoSuchFieldException
*/
public static Object readByFile(Object object, JSONReader jsonReader) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException{
Class<? extends Object> model = object.getClass();
//开始解析json对象
jsonReader.startObject();
while(jsonReader.hasNext()) {
String key = jsonReader.readString();
Field field = model.getDeclaredField(key);
if (field != null) {
//属性名称
String fieldName = field.getName();
//属性类型
Class<?> fieldType = field.getType();
if (field.getName().equals(key)) {
//执行set方法
invokeSetMethod(fieldName,fieldType,model,object,jsonReader.readString());
} else {
jsonReader.readObject(); //读取对象,保证在添加字段之后不会因为文件中和对象字段对不上出错
}
}
}
//结束解析对象
jsonReader.endObject();
return object;
}
/**
* 跟据文件路径和文件名生成jsonWriter
* @param jsonPath 文件路径
* @param fileName 文件名称
* @return
* @throws UnsupportedEncodingException
* @throws FileNotFoundException
*/
public static JSONWriter getJsonWrite(String jsonPath, String fileName) throws UnsupportedEncodingException, FileNotFoundException {
File dir = new File(jsonPath);
if (!dir.exists())
dir.mkdirs();
File jsonFile = new File(dir + File.separator + fileName + ".dat");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(jsonFile),"utf-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
JSONWriter jsonWriter = new JSONWriter(bufferedWriter);
return jsonWriter;
}
//拼接并执行get方法
private static Object invokeGetMethod(String fieldName, Class<? extends Object> model, Object object) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//拼接方法名
String firstCharUpper = fieldName.substring(0, 1).toUpperCase();
String invokeMethodName = "get" + firstCharUpper + fieldName.substring(1);
//获取方法
Method pendingInvokeMethod = model.getMethod(invokeMethodName);
//执行方法
Object invokeResult = pendingInvokeMethod.invoke(object);
return invokeResult;
}
//拼接并执行set方法
private static void invokeSetMethod(String fieldName, Class<?> fieldType, Class<? extends Object> model, Object object,String readString) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//拼接方法名
String firstCharUpper = fieldName.substring(0, 1).toUpperCase();
String invokeMethodName = "set" + firstCharUpper + fieldName.substring(1);
if(fieldType.isAssignableFrom(String.class)) {
//获取方法
Method pendingInvokeMethod = model.getMethod(invokeMethodName,String.class);
//执行方法
pendingInvokeMethod.invoke(object, readString);
} else if (fieldType.isAssignableFrom(Date.class)) {
//获取方法
Method pendingInvokeMethod = model.getMethod(invokeMethodName,Date.class);
pendingInvokeMethod.invoke(object, DateUtils.parseDate(readString));
}
}