JavaBean中数据写入文件工具类
1 import java.beans.PropertyDescriptor; 2 import java.io.BufferedReader; 3 import java.io.BufferedWriter; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.io.Writer; 10 import java.lang.reflect.Field; 11 import java.lang.reflect.Method; 12 import java.util.ArrayList; 13 import java.util.List; 14 15 import org.apache.commons.io.output.FileWriterWithEncoding; 16 17 import com.genilex.gRule.ocr.dto.DrivingLicense; 18 19 public class FileUtil { 20 private FileUtil(){} 21 22 /** 23 * 实体类数据写入文件中 24 * @param <T> 25 * @param t 实体类 26 * @param fileName 文件名(带后缀) 27 * @param fileDir 文件夹路径,不包括文件名及后缀 28 * @return 29 */ 30 public static <T> boolean beanToFile(T t,String fileName,String fileDir){ 31 boolean bl = false; 32 try { 33 //取得类中所有属性 34 Class clazz = t.getClass(); 35 Field[] fields = clazz.getDeclaredFields(); 36 37 //判断文件夹是否存在,不存在则创建 38 File fileDir2 = new File(fileDir); 39 if(!fileDir2.exists()){ 40 fileDir2.mkdirs(); 41 } 42 //文件 43 File file = new File(fileDir,fileName); 44 45 //需要封装的list 46 List<String> list = new ArrayList<String>(); 47 //header数组,用来设置数据字符串,使其一一按顺序对应 48 String[] titles = null; 49 if(!file.exists()){//如果文件不存在,则创建文件并加header 50 //header字符串设置 51 StringBuilder titleStr = new StringBuilder(); 52 for (int i = 0; i < fields.length; i++) { 53 if(i > 0){ 54 titleStr.append(","); 55 } 56 titleStr.append(fields[i].getName()); 57 } 58 list.add(titleStr.toString()); 59 60 titles = titleStr.toString().split(","); 61 }else{//如果文件存在,则继续追加数据 62 //读取文件第一行获得header 63 titles = readFirstLine(file).replace("\"","").split(","); 64 } 65 //封装数据字符串,与header按顺序对于设置 66 list.add(beanToStr(t,titles,clazz)); 67 bl = listToFile(list,file); 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } 71 return bl; 72 } 73 74 /** 75 * 根据传入的key数组设置bean中数据,按key的顺序封装成字符串 76 * @param obj javaBean 77 * @param keys 列名数组 78 * @param clazz Class 79 * @return 80 */ 81 public static String beanToStr(Object obj,String[] titles,Class<?> clazz){ 82 try { 83 StringBuilder sb = new StringBuilder(); 84 85 for (int i = 0; i < titles.length; i++) { 86 PropertyDescriptor pd = new PropertyDescriptor(titles[i], clazz); 87 //得到get方法 88 Method method = pd.getReadMethod(); 89 //执行get方法 90 Object value = method.invoke(obj); 91 if(i > 0){ 92 sb.append(","); 93 } 94 //封装数据,用","隔开 95 sb.append(value != null ? String.valueOf(value) : ""); 96 } 97 return sb.toString(); 98 } catch (Exception e) { 99 e.printStackTrace(); 100 return null; 101 } 102 } 103 104 /** 105 * 读取文件第一行 106 * @param file 107 * @return 108 */ 109 public static String readFirstLine(File file){ 110 String res = null; 111 112 InputStream is = null; 113 BufferedReader buffer = null; 114 try { 115 is = new FileInputStream(file); 116 buffer = new BufferedReader(new InputStreamReader(is,"UTF-8")); 117 118 res = buffer.readLine(); 119 } catch (Exception e) { 120 e.printStackTrace(); 121 }finally{ 122 if(buffer != null){ 123 try { 124 buffer.close(); 125 } catch (IOException e) { 126 e.printStackTrace(); 127 buffer = null; 128 } 129 } 130 if(is != null){ 131 try { 132 is.close(); 133 } catch (IOException e) { 134 e.printStackTrace(); 135 is = null; 136 } 137 } 138 } 139 return res; 140 } 141 142 /** 143 * list 写入文件 144 * @param list 145 * @param file 146 * @return 147 */ 148 public static boolean listToFile(List<String> list,File file){ 149 boolean bl = true; 150 151 Writer writer = null; 152 BufferedWriter buffer = null; 153 try { 154 writer = new FileWriterWithEncoding(file,"UTF-8",true); 155 buffer = new BufferedWriter(writer); 156 157 if(list != null && !list.isEmpty()){ 158 for (String str : list) { 159 buffer.write(str); 160 buffer.newLine(); 161 } 162 } 163 } catch (Exception e) { 164 e.printStackTrace(); 165 bl = false; 166 }finally{ 167 if(buffer != null){ 168 try { 169 buffer.close(); 170 } catch (IOException e) { 171 e.printStackTrace(); 172 buffer = null; 173 } 174 } 175 if(writer != null){ 176 try { 177 writer.close(); 178 } catch (IOException e) { 179 e.printStackTrace(); 180 writer = null; 181 } 182 } 183 } 184 return bl; 185 } 186 187 public static void main(String[] args) { 188 DrivingLicense dl = new DrivingLicense(); 189 dl.setVin("你"); 190 dl.setAddress("好"); 191 dl.setEngineNo("啊"); 192 193 beanToFile(dl, "xingshizheng.csv", "D:\\xingshizeng"); 194 } 195 }