數據記錄必須以下格式(且最後一個逗號分隔符的右邊不能爲空)java
OUB_SHIPMENT_HEADER_ID,SHIPMENT_NO,STATUS,STATUS_EXT,PRIORITYapi
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; } }