数据记录必须如下格式(且最后一个逗号分隔符的右边不能为空)
OUB_SHIPMENT_HEADER_ID,SHIPMENT_NO,STATUS,STATUS_EXT,PRIORITY
12313412416491,GF1712262040050,100,0
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.test.data_provide.entity.OubShipmentHeader; public class Sql2Entity { List<String> list= new ArrayList<String>(); List<String> listone= new ArrayList<String>(); List<String> listtwo= new ArrayList<String>(); public void toEntity(File rfile,File wfile,Class<?> obj,String entityInstance) throws IOException{ //读取每一行到list List<String> rlist=readTxt(rfile); int count=0; //循环读取list的值 for(String l:rlist){ //"," split掉,有序的 String[] lArray=l.split(","); //再循环处理字符串数组的每个值 for(int i=0;i<lArray.length;i++){ StringBuilder sb = new StringBuilder(); //处理第一行 if(count==0){ //再"_"split掉,有序的 String[] tArray= lArray[i].split("_"); //再循环处理字符串的每个值,首字母大写,再拼接 for(int j=0;j<tArray.length;j++){ sb.append(preWordCapital(tArray[j])); } listone.add(sb.toString()); } //处理第二行value值 if(count==1){ listtwo.add(lArray[i]); } } count++; } //拼接set方法 writeTxt(wfile,concatList(listone,listtwo,obj,entityInstance)); //writeTxt(wfile,listone); //writeTxt(wfile,listtwo); } public void writeTxt(File file,List<String> list) throws IOException{ FileWriter fw = new FileWriter(file,false); BufferedWriter bw = new BufferedWriter(fw); for(String l:list){ bw.write(l); bw.write("\r"); } bw.close(); fw.close(); } public static void main(String[] args) throws IOException { File rfile = new File("D:/mytest1.txt"); File wfile = new File("D:/entity.txt"); Sql2Entity se= new Sql2Entity(); se.toEntity(rfile, wfile, OubShipmentHeader.class,"oubShipmentHeader"); /*se.readTxt(rfile); List<String> list = new ArrayList<String>(); list.add("wode"); list.add("ren"); se.writeTxt(wfile,list);*/ } public List<String> readTxt(File file){ List<String> list= new ArrayList<String>(); StringBuilder result = new StringBuilder(); try{ BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件 String s = null; while((s = br.readLine())!=null){//使用readLine方法,一次读一行 //result.append(System.lineSeparator()+s); //System.out.println(s+"\n"); list.add(s); } br.close(); }catch(Exception e){ e.printStackTrace(); } return list; } public String preWordCapital(String word){ String lowerWord = word.toLowerCase(); return lowerWord.substring(0, 1).toUpperCase() + lowerWord.substring(1); } public List<String> concatList(List<String> listone,List<String> listtwo,Class<?> obj,String objInstance){ Field[] field = obj.getDeclaredFields(); System.out.println("listone.size() "+listone.size()+" listtwo.size(): "+listtwo.size()); if(listone.size()!=listtwo.size()){ System.err.println("字段与值的个数不对应,请检查是否最后一个,分割符右边为空,必须保证最后一个,分割符右边不为空..."); } for(int i=0;i<listone.size();i++){ int j=0; for (j = 0; j < field.length; j++) { field[j].setAccessible(true); //System.out.println(field[j].getName()); if(field[j].getName().toString().equalsIgnoreCase(listone.get(i))){ //System.out.println(listone.get(i)); //System.out.println(field[j].getName().toString()); break; } } // 获取属性的类型 String type = field[j].getGenericType().toString(); //String类型拼接 if(type.equals("class java.lang.String")){ list.add(objInstance+".set"+listone.get(i)+"("+"\""+listtwo.get(i)+"\""+")"); } //Int类型拼接 if(type.equals("class java.lang.Integer")){ list.add(objInstance+".set"+listone.get(i)+"("+listtwo.get(i)+")"); } //Long类型拼接 if(type.equals("class java.lang.Long")){ list.add(objInstance+".set"+listone.get(i)+"("+listtwo.get(i)+"l"+")"); } //BigDecimal类型拼接 if(type.equals("class java.math.BigDecimal")){ list.add(objInstance+".set"+listone.get(i)+"("+"parames.getBigDecimalParams("+listtwo.get(i)+")"+")"); } //Data类型拼接 if(type.equals("class java.util.data")){ list.add(objInstance+".set"+listone.get(i)+"("+"parames.getDateParams()"+")"); } //Byte类型拼接 if(type.equals("class java.lang.Byte")){ list.add(objInstance+".set"+listone.get(i)+"("+"parames.getByteParams("+listtwo.get(i)+")"+")"); } } return list; } }
原文地址:https://www.cnblogs.com/xianlai-huang/p/8126062.html
时间: 2024-10-16 11:43:13