要制作一个slg游戏,因为通常slg游戏内容过长,就必须对内容进行持久化保存
之前在1.自定义游戏地图格式的设定,加载,保存中是直接用java的FileOutputStream,DataOutputStream两个类的方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public static void saveMapBin(MapBinDAO binFile, String Path) { try { FileOutputStream fs_out = new FileOutputStream(Path);//"D://test.bin" DataOutputStream out = new DataOutputStream(fs_out); out.writeShort(binFile.mapVersion);// 4 out.writeInt(binFile.mapWidth);// 8 out.writeInt(binFile.mapHeight);// 8 for (int i = 0; i < binFile.getMapbin().size(); i++) { out.writeByte(binFile.getMapbin().get(i).getBlockType());// 2 out.writeByte(binFile.getMapbin().get(i).getBackTile());// 2 out.writeByte(binFile.getMapbin().get(i).getBackIdx());// 2 out.writeByte(binFile.getMapbin().get(i).getBackRefX());// 2 out.writeByte(binFile.getMapbin().get(i).getBackRefY());// 2 out.writeByte(binFile.getMapbin().get(i).getForeTile());// 2 out.writeByte(binFile.getMapbin().get(i).getForeIdx());// 2 out.writeByte(binFile.getMapbin().get(i).getForeRefX());// 2 out.writeByte(binFile.getMapbin().get(i).getForeRefY());// 2 out.writeByte(binFile.getMapbin().get(i).getWaterPass());// 2 out.writeByte(binFile.getMapbin().get(i).getLandPass());// 2 out.writeInt(binFile.getMapbin().get(i).getRegionId());// 8 out.writeByte(binFile.getMapbin().get(i).getClimateId());// 2 } out.close(); // out.writeInt(i);//4位 // out.writeShort(i2);//2位 // out.writeByte(i3);//1位 // out.close(); } catch (FileNotFoundException fe) { System.err.println(fe); } catch (IOException ioe) { System.err.println(ioe); } System.out.println("Ok"); }
现在要改成用libgdx的方法保存
这里我是在制作地图编辑器的过程中遇到这个需求,保存修改后的地图
这里我写了一个FileByte类,来模拟DataOutputStream类来保存byte数据
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.zhfy.game.framework.tool; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; public class FileByte { private int bytesLength; private List<Integer> bytes; public void init() { bytes=new ArrayList<Integer>(); bytesLength=0; } //4 public final void writeInt(int v) throws IOException { if(bytes==null) { init(); } bytes.add((v >>> 24) & 0xFF); bytes.add((v >>> 16) & 0xFF); bytes.add((v >>> 8) & 0xFF); bytes.add((v >>> 0) & 0xFF); incCount(4); } //2 public final void writeShort(int v) throws IOException { if(bytes==null) { init(); } bytes.add((v >>> 8) & 0xFF); bytes.add((v >>> 0) & 0xFF); incCount(2); } //1 public final void writeByte(int v) throws IOException { if(bytes==null) { init(); } bytes.add(v); incCount(1); } private void incCount(int value) { int temp = bytesLength + value; if (temp < 0) { temp = Integer.MAX_VALUE; } bytesLength = temp; } //TODO 待验证 public byte[] getByte(){ byte[] bLocalArr = new byte[bytesLength]; int i,iMax;iMax=bytes.size(); for ( i = 0; i<iMax; i++) { bLocalArr[i] = (byte) (bytes.get(i)& 0xFF); } init(); return bLocalArr; } }
然后保存方法修改为
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public static void saveMapBin(MapBinDAO binFile, String Path) { try { FileByte out=new FileByte(); out.writeShort(binFile.mapVersion);// 4 out.writeInt(binFile.mapWidth);// 8 out.writeInt(binFile.mapHeight);// 8 for (int i = 0; i < binFile.getMapbin().size(); i++) { out.writeByte(binFile.getMapbin().get(i).getBlockType());// 2 out.writeByte(binFile.getMapbin().get(i).getBackTile());// 2 out.writeByte(binFile.getMapbin().get(i).getBackIdx());// 2 out.writeByte(binFile.getMapbin().get(i).getBackRefX());// 2 out.writeByte(binFile.getMapbin().get(i).getBackRefY());// 2 out.writeByte(binFile.getMapbin().get(i).getForeTile());// 2 out.writeByte(binFile.getMapbin().get(i).getForeIdx());// 2 out.writeByte(binFile.getMapbin().get(i).getForeRefX());// 2 out.writeByte(binFile.getMapbin().get(i).getForeRefY());// 2 out.writeByte(binFile.getMapbin().get(i).getWaterPass());// 2 out.writeByte(binFile.getMapbin().get(i).getLandPass());// 2 out.writeInt(binFile.getMapbin().get(i).getRegionId());// 8 out.writeByte(binFile.getMapbin().get(i).getClimateId());// 2 } FileHandle file = Gdx.files.local(Path); file.writeBytes(out.getByte(), false); // out.writeInt(i);//4位 // out.writeShort(i2);//2位 // out.writeByte(i3);//1位 // out.close(); } catch (FileNotFoundException fe) { System.err.println(fe); } catch (IOException ioe) { System.err.println(ioe); } System.out.println("Ok"); }
在游戏中提示游戏保存成功
但是通过Everything这个搜索工具发现地址并没在预想的位置,如图的顶部和底部,底部是原文件位置,顶部是保存后生成的所在位置
主要原因是
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
// 根据mapid生成MapBinDAO类 public MapBinDAO getMapDaoByMapId(int mapId) { XmlReader reader = new XmlReader(); String str = ""; Element root = reader.parse(Gdx.files.internal("config/def_map.xml")); int childNum = root.getChildCount(); for (int i = 0; i < childNum; i++) { if (root.getChild(i).getInt("id") == mapId) { str = root.getChild(i).get("name"); bt = Gdx.files.internal("bin/" + str + ".bin").readBytes(); try { mapBinDao = GameMap.readMapBin(bt); } catch (IOException e) { e.printStackTrace(); } return mapBinDao; } } return mapBinDao; }
保存位置用的方法是local,而我们加载资源时用的方法是internal方法
通过查资料得知
internal位置为只读位置
而local位置为可读可写的位置
并且专门实验了下,使用internal方法保存会出错,
所以这里把加载位置改为local,并放入desktop对应位置,在正式发布的时候再把文件放入android对应assets位置
---------------------------4.19补充------------------------------
明确下长度规则
//out.writeInt(i);//8位 2147483647
//out.writeShort(i2);//4位 32769
//out.writeByte(i3);//2位 127
---------------------------7.2补充--------------------------------
通过界限计划我已经实现了按照配置读取,保存数据,
但是由于打包安卓导入jar包失败,所以重写了一个不引用jar包(主要是用fastJson包,现在用map取代了json)的读取版本
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package loadRule; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.XmlReader; import com.badlogic.gdx.utils.XmlReader.Element; import dao.BTLDAO; import dao.DefRule; import dao.FileByte; import dao.module.BtlModule0; import dao.module.BtlModule1; import dao.module.BtlModule10; import dao.module.BtlModule11; import dao.module.BtlModule12; import dao.module.BtlModule13; import dao.module.BtlModule14; import dao.module.BtlModule15; import dao.module.BtlModule16; import dao.module.BtlModule17; import dao.module.BtlModule18; import dao.module.BtlModule19; import dao.module.BtlModule2; import dao.module.BtlModule20; import dao.module.BtlModule3; import dao.module.BtlModule4; import dao.module.BtlModule5; import dao.module.BtlModule6; import dao.module.BtlModule7; import dao.module.BtlModule8; import dao.module.BtlModule9; public class loadRule { public static void main(String[] args) { BTLDAO btl = null; long startTime = System.currentTimeMillis(); try { btl= loadBTLByRule("E:\\rule_fb1_btl.xml", "E:\\conquest1.btl"); } catch (Exception e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); //获取结束时间 System.out.println("程序加载时间:" + (endTime - startTime) + "ms"); //输出程序运行时间 long startTime2 = System.currentTimeMillis(); try { saveMapBin(btl,"E:\\rule_fb1_btl.xml","E:\\conquest1_test.btl"); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long endTime2 = System.currentTimeMillis(); //获取结束时间 System.out.println("程序保存时间:" + (endTime2 - startTime2) + "ms"); //输出程序运行时间 System.out.println("ok"); } //将rule处理为map //rsInfo {bm0={remark=主数据 128, id=bm0, Count=one, ifCycle=false}} //rsDetail {bm0_1={DefRule}} public static Map getMapByRule(String rulePath) { //TODO need deal to gdxMethod String ruleContent = readToString(rulePath); XmlReader reader = new XmlReader(); Element root = reader.parse(ruleContent); Map rsInfoMap = new HashMap();//存放ifCycle,Count,remark Map rsDetailMap = new HashMap();//存放每一条规则 Map rsFunctionMap = new HashMap();//存放function的id Map rsMap = new HashMap(); Array<Element> authors = root.getChildrenByNameRecursively("Files"); int ruleSize = 0; //System.out.println("root.name:" + root.getName()); String[] mapStrs = new String[] { "bm0", "bm1", "bm2", "bm3", "bm4", "bm5", "bm6", "bm7", "bm8", "bm9", "bm10", "bm11", "bm12", "bm13", "bm14", "bm15", "bm16", "bm17", "bm18", "bm19", "bm20" }; for (int i = 0; i < mapStrs.length; i++) { Element bmElement = root.getChildByName(mapStrs[i]); if (bmElement != null) { Array<Element> bmElements = bmElement.getChildrenByNameRecursively("bm"); ruleSize = 0; for (Element bm : bmElements) { ruleSize = ruleSize + 1; DefRule rule = new DefRule(); rule.setId(bm.get("id")); rule.setSize(bm.getInt("size")); rule.setName(bm.get("name")); rule.setRemark(bm.get("remark")); rule.setType(bm.get("type")); rule.setFunction(bm.get("function")); rule.setDefaul(bm.get("defaul")); rsDetailMap.put(rule.getId(), rule); if (!rule.getFunction().equals("no") && !rule.getFunction().equals("")) { rsFunctionMap.put(rule.getFunction(), rule.getId()); //System.out.println(rule.getFunction()+":"+rule.getId()); } } Map tempMap = new HashMap(); tempMap.put("id", mapStrs[i]); tempMap.put("ifCycle", bmElement.get("ifCycle")); tempMap.put("Count", bmElement.get("Count")); tempMap.put("remark", bmElement.get("remark")); tempMap.put("Size", ruleSize);//存放该子条目的条数 rsInfoMap.put(mapStrs[i], tempMap); } } rsMap.put("rsInfo", rsInfoMap); rsMap.put("rsDetail", rsDetailMap); rsMap.put("rsFunction", rsFunctionMap); return rsMap; } //切割btl根据xml的记录 rsDetail, bi, buf, bufTag, cutSumCt //等待实际测试,如果结果不理想再去泛型 private static <T> Map<String, Object> cutBtl(Map rs, T item, StringBuilder buf, int bufTag, int cycleCount) { Map<String, Object> rsMap = new HashMap<String, Object>(); List<T> ts = new ArrayList<T>(); Class clazz = item.getClass(); Field[] fieldName; Class clazs; Field f = null; int beginBufTag = bufTag; int cutL, c; String str = null; for (c = 0; c < cycleCount; c++) { fieldName = clazz.getDeclaredFields(); try { item = (T) clazz.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } clazs = item.getClass(); //for (n = 0; n < ruleSize; n++) { for (int i = 0; i < fieldName.length; i++) { // 创建实例 try { f = clazs.getDeclaredField(fieldName[i].getName()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } f.setAccessible(true); if (rs.containsKey(fieldName[i].getName().toString())) { DefRule r = (DefRule) rs.get(fieldName[i].getName().toString()); //System.out.println("B:Name1:"+r.getId()+" c:"+c+" bs:"+bufTag); str = getCoverStr(r, buf, bufTag); bufTag = bufTag + r.getSize(); try { f.set(item, str); } catch (Exception e) { e.printStackTrace(); } } } //} if (item != null) { ts.add(item); } } if (cycleCount == 1 && beginBufTag == 0) { rsMap.put("T", item); } else { removeNull(ts); rsMap.put("T", ts); } rsMap.put("bufTag", bufTag); return rsMap; } //将rule处理为map //rsInfo {bm0={remark=主数据 128, id=bm0, Count=one, ifCycle=false}} //rsDetail {bm0_1={DefRule}} //使用gdx的方法根据rule读取数据,处理为到dao类 public static BTLDAO loadBTLByRule(String rulePath, String filePath) throws Exception { //将rule处理为map Map ruleMap = getMapByRule(rulePath); Map rsInfoMap = (Map) ruleMap.get("rsInfo"); Map rsDetailMap = (Map) ruleMap.get("rsDetail"); Map rsFunctionMap = (Map) ruleMap.get("rsFunction"); byte[] rsbt = null; String cutStr = ""; int bufTag = 0; int cutSumCt = 1;//总循环次数 int mapW = 0, mapH = 0, i; Map rsMap = null; Map biMap = null; Map row; int line = 0; BTLDAO btl = new BTLDAO(); StringBuilder buf = new StringBuilder(); {//读取bin try { rsbt = readFile(filePath);//"D:\\test1.bin" } catch (IOException e) { e.printStackTrace(); } // 十六进制标记 for (byte d : rsbt) { if (line % 1 == 0) { buf.append(String.format("%02x", d)); line++; } } } { //得到基础信息 cutStr = "bm0"; BtlModule0 bi = new BtlModule0(); rsMap = cutBtl(rsDetailMap, bi, buf, bufTag, cutSumCt); bi = (BtlModule0) rsMap.get("T"); btl.setBm0(bi); bufTag = Integer.parseInt(rsMap.get("bufTag").toString()); biMap = JBeanToMap(bi); if (rsFunctionMap.containsKey("mapH")) { mapH = Integer.parseInt(biMap.get(rsFunctionMap.get("mapH")).toString()); } if (rsFunctionMap.containsKey("mapW")) { mapW = Integer.parseInt(biMap.get(rsFunctionMap.get("mapW")).toString()); } if (mapW * mapH == 0) { throw new Exception("没有得到有效的宽高"); } /**/ } { //重复读取所有基本信息 Object[] objects = new Object[] { (new BtlModule1()), (new BtlModule2()), (new BtlModule3()), (new BtlModule4()), (new BtlModule5()), (new BtlModule6()), (new BtlModule7()), (new BtlModule8()), (new BtlModule9()), (new BtlModule10()), (new BtlModule11()), (new BtlModule12()), (new BtlModule13()), (new BtlModule14()), (new BtlModule15()), (new BtlModule16()), (new BtlModule17()), (new BtlModule18()), (new BtlModule19()), (new BtlModule20()) }; String[] cutStrs = new String[] { "bm1", "bm2", "bm3", "bm4", "bm5", "bm6", "bm7", "bm8", "bm9", "bm10", "bm11", "bm12", "bm13", "bm14", "bm15", "bm16", "bm17", "bm18", "bm19", "bm20" }; boolean tr = false; boolean ifCtEq0 = false; int c; for (i = 0; i < objects.length; i++) { if (rsInfoMap.containsKey(cutStrs[i])) { row = (Map) rsInfoMap.get(cutStrs[i]); if (row.get("ifCycle").equals("true") || row.get("ifCycle").equals("false")) { tr = row.get("ifCycle") .equals( "true"); } else { if (row.get("ifCycle").toString().indexOf("==") != -1) { c = getCharacterPosition(row.get("ifCycle").toString(), "==", 1); if (c != -1) { try { tr = biMap.get(row.get("ifCycle").toString().substring(0, c)).equals(row.get("ifCycle").toString().substring(c + 2)); } catch (Exception e) { //e.printStackTrace(); } if (!tr) { ifCtEq0 = true; } } } else if (row.get("ifCycle").toString().indexOf("!=") != -1) { c = getCharacterPosition(row.get("ifCycle").toString(), "!=", 1); if (c != -1) { try { tr = !biMap.get(row.get("ifCycle").toString().substring(0, c)).equals(row.get("ifCycle").toString().substring(c + 2)); } catch (Exception e) { //e.printStackTrace(); } if (!tr) { ifCtEq0 = true; } } } } if (tr) { if (row.get("Count").equals("one")) { cutSumCt = 1; } else if (row.get("Count").equals("sumGride")) { cutSumCt = mapW * mapH; } else { cutSumCt = Integer.parseInt((String) biMap.get(row.get("Count"))); } } else { if (ifCtEq0) { cutSumCt = 0; } else { cutSumCt = 1; } } rsMap = cutBtl(rsDetailMap, objects[i], buf, bufTag, cutSumCt); setVal(btl, "set" + UpperInitial(cutStrs[i]), rsMap.get("T")); bufTag = Integer.parseInt(rsMap.get("bufTag").toString()); } } } {//是否有遗漏数据 String str= buf.substring(bufTag); if(!str.trim().equals("")) { throw new Exception("剩余未解析数据为:"+str); } } return btl; } //读取文本内容,暂时替代gdx的读取方法 public static String readToString(String fileName) { String encoding = "UTF-8"; File file = new File(fileName); Long filelength = file.length(); byte[] filecontent = new byte[filelength.intValue()]; try { FileInputStream in = new FileInputStream(file); in.read(filecontent); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { return new String(filecontent, encoding); } catch (UnsupportedEncodingException e) { System.err.println("The OS does not support " + encoding); e.printStackTrace(); return null; } } //根据规则解析map生成bin public static void saveMapBin(BTLDAO btl,String rule, String path) throws NumberFormatException, IOException { Map ruleMap = getMapByRule(rule); Map rsInfoMap = (Map) ruleMap.get("rsInfo"); Map rsDetailMap = (Map) ruleMap.get("rsDetail"); Map rsFunctionMap = (Map) ruleMap.get("rsFunction"); Map map=null; try { map = getBtlMap(btl); } catch (Exception e1) { e1.printStackTrace(); } Map row; int i, s; List<DefRule> rs; List<Map> list; Map m = null; String temStr = null; //StringBuilder buf = new StringBuilder(); FileByte out = new FileByte(); String[] cutStrs = new String[] { "bm0", "bm1", "bm2", "bm3", "bm4", "bm5", "bm6", "bm7", "bm8", "bm9", "bm10", "bm11", "bm12", "bm13", "bm14", "bm15", "bm16", "bm17", "bm18", "bm19", "bm20" }; boolean tr = false; boolean ifCtEq0 = false; int c;int ruleSize=0,n=0;String tempBmId; for (i = 0; i < cutStrs.length; i++) { if (rsInfoMap.containsKey(cutStrs[i])) { row = (Map) rsInfoMap.get(cutStrs[i]); ruleSize=(int) ((Map) rsInfoMap.get(cutStrs[i])).get("Size"); if (row.get("ifCycle").equals("true") || row.get("ifCycle").equals("false")) { tr = row.get("ifCycle").equals( "true"); } else { if (row.get("ifCycle").toString().indexOf("==") != -1) { c = getCharacterPosition(row.get("ifCycle").toString(), "==", 1); if (c != -1) { try { tr = ((Map) map.get("bm0")).get(row.get("ifCycle").toString().substring(0, c)).equals(row.get("ifCycle").toString().substring(c + 2)); } catch (Exception e) { //e.printStackTrace(); } if (!tr) { ifCtEq0 = true; } } } else if (row.get("ifCycle").toString().indexOf("!=") != -1) { c = getCharacterPosition(row.get("ifCycle").toString(), "!=", 1); if (c != -1) { try { tr = !((Map) map.get("bm0")).get(row.get("ifCycle").toString().substring(0, c)).equals(row.get("ifCycle").toString().substring(c + 2)); } catch (Exception e) { //e.printStackTrace(); } if (!tr) { ifCtEq0 = true; } } } } if (tr) { list = (List<Map>) map.get(cutStrs[i]); for (Map lMap : list) { for (n=1;n<=ruleSize;n++) { tempBmId=cutStrs[i]+"_"+n; if (lMap.containsKey(tempBmId)) { DefRule r=(DefRule) rsDetailMap.get(tempBmId); writeFile(out,r, lMap); //buf.append(temStr); } } } } else { if (!ifCtEq0 && map.get(cutStrs[i]) instanceof List) { m = (Map) ((List) map.get(cutStrs[i])).get(0); } else if (!ifCtEq0) { m = (Map) map.get(cutStrs[i]); for (n=1;n<=ruleSize;n++) { tempBmId=cutStrs[i]+"_"+n; DefRule r=(DefRule) rsDetailMap.get(tempBmId); writeFile(out,r, m); //buf.append(temStr); } } } } } { FileOutputStream fs; try { fs = new FileOutputStream(path); fs.write(out.getByte()); fs.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } //ComUtil public static byte[] readFile(String file) throws IOException { InputStream is = new FileInputStream(file); int length = is.available(); byte bt[] = new byte[length]; is.read(bt); return bt; } //List 集合去除null元素 ComUtil public static <T> List<T> removeNull(List<? extends T> oldList) { // 你没有看错,真的是有 1 行代码就实现了 oldList.removeAll(Collections.singleton(null)); return (List<T>) oldList; } public static String getCoverStr(DefRule r, StringBuilder buf, int bufTag) { int cutL = r.getSize(); int str = 0; if (cutL != 0) { str = Integer.parseInt(buf.substring(bufTag, bufTag + cutL), 16); } return str + ""; } public static void writeFile(FileByte out,DefRule r, Map m) throws NumberFormatException, IOException { String temStr; int cutL = r.getSize(); if (m.containsKey(r.getId())) { if (r.getSize()==2) { out.writeByte(Integer.parseInt(m.get(r.getId()).toString())); }else if(r.getSize()==4) { out.writeShort(Integer.parseInt(m.get(r.getId()).toString())); }else if(r.getSize()==8) { out.writeInt(Integer.parseInt(m.get(r.getId()).toString())); } } else { if (r.getSize()==2) { out.writeByte( Integer.parseInt( r.getDefaul())); }else if(r.getSize()==4) { out.writeShort( Integer.parseInt( r.getDefaul())); }else if(r.getSize()==8) { out.writeInt( Integer.parseInt( r.getDefaul())); } } } //对象转Map ComUtil public static Map JBeanToMap(Object obj) { Map<String, Object> reMap = new HashMap<String, Object>(); if (obj == null) return null; Field[] fields = obj.getClass().getDeclaredFields(); try { for (int i = 0; i < fields.length; i++) { try { Field f = obj.getClass().getDeclaredField(fields[i].getName()); f.setAccessible(true); Object o = f.get(obj); reMap.put(fields[i].getName(), o); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } catch (SecurityException e) { e.printStackTrace(); } return reMap; } //ComUtil public static int getCharacterPosition(String url, String s, int i) { //这里是获取"/"符号的位置 lastindexof从字符串末尾开始检索,检索到子字符 Matcher slashMatcher = Pattern.compile(s).matcher(url); int mIdx = 0; while (slashMatcher.find()) { mIdx++; //当"/"符号第i次出现的位置 if (mIdx == i) { break; } } int rs; try { rs = slashMatcher.start(); } catch (Exception e) { rs = -1; } return rs; } //ComUtil //示例 setVal(obj,"setUpdateUser","修改人"); 给对象赋值 public static void setVal(Object obj, String methodName, Object value) { if (value == null || obj == null) { return; } String method_name = methodName; Method[] methods = obj.getClass().getMethods(); for (Method method : methods) { /** * 因为这里只是调用bean中属性的set方法,属性名称不能重复 所以set方法也不会重复,所以就直接用方法名称去锁定一个方法 * (注:在java中,锁定一个方法的条件是方法名及参数) **/ if (method.getName().equals(method_name)) { Class[] parameterC = method.getParameterTypes(); try { /** * 如果是基本数据类型时(如int、float、double、byte、char、boolean) * 需要先将Object转换成相应的封装类之后再转换成对应的基本数据类型 否则会报 * ClassCastException **/ if (parameterC[0] == int.class) { method.invoke(obj, ((Integer) value).intValue()); break; } else if (parameterC[0] == float.class) { method.invoke(obj, ((Float) value).floatValue()); break; } else if (parameterC[0] == double.class) { method.invoke(obj, ((Double) value).doubleValue()); break; } else if (parameterC[0] == byte.class) { method.invoke(obj, ((Byte) value).byteValue()); break; } else if (parameterC[0] == char.class) { method.invoke(obj, ((Character) value).charValue()); break; } else if (parameterC[0] == boolean.class) { method.invoke(obj, ((Boolean) value).booleanValue()); break; } else { method.invoke(obj, parameterC[0].cast(value)); break; } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } } } //ComUtil //将第一个字母大写 public static String UpperInitial(String str) { if (str != null && str != "") { str = str.substring(0, 1).toUpperCase() + str.substring(1); } return str; } //读取btl,转为map public static Map getBtlMap(BTLDAO binFile) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { int i; Object om; Map map = null; List<Map<String, Object>> tempMaps; Map<String, Object> rsMap = new HashMap(); Object s; {//遍历btl Field[] fields = binFile.getClass().getDeclaredFields();//Object是已经被赋值的对象实例 for (Field field : fields) { if (!field.isAccessible()) { field.setAccessible(true); } //如果是list类 if (List.class.isAssignableFrom(field.getType())) { Type t = field.getGenericType(); if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; Class clz = (Class) pt.getActualTypeArguments()[0];//得到对象list中实例的类型 if (field.get(binFile) != null) { Class clazz = field.get(binFile).getClass();//获取到属性的值的Class对象 Method m = clazz.getDeclaredMethod("size"); int size = (Integer) m.invoke(field.get(binFile));//调用list的size方法,得到list的长度 tempMaps = new ArrayList<Map<String, Object>>(); for (int i2 = 0; i2 < size; i2++) {//遍历list,调用get方法,获取list中的对象实例 Method getM = clazz.getDeclaredMethod("get", int.class); if (!getM.isAccessible()) { getM.setAccessible(true); s = getM.invoke(field.get(binFile), i2); map = getKeyAndValue(s); tempMaps.add(map); } } rsMap.put(field.getName(), tempMaps); } } } else {//否则为普通类 field.setAccessible(true); Object value = field.get(binFile); map = getKeyAndValue(value); rsMap.put(field.getName(), map); } } } return rsMap; } //获取单个对象的值 ComUtil public static Map<String, Object> getKeyAndValue(Object obj) { Map<String, Object> map = new HashMap<String, Object>(); // 得到类对象 Class userCla = (Class) obj.getClass(); /* 得到类中的所有属性集合 */ Field[] fs = userCla.getDeclaredFields(); for (int i = 0; i < fs.length; i++) { Field f = fs[i]; f.setAccessible(true); // 设置些属性是可以访问的 Object val = new Object(); try { val = f.get(obj); // 得到此属性的值 if (val != null) { map.put(f.getName(), val);// 设置键值 } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } //System.out.println("单个对象的所有键值==反射==" + map.toString()); return map; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package dao; import java.util.ArrayList; import java.util.List; public class DefRule { private String id; private String name; private String type;//String,Long,Byte,Integer,Single private String remark; private String defaul;//由于关键字 所有换为defaul private int size; private String function;//no public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getDefaul() { return defaul; } public void setDefaul(String defaul) { this.defaul = defaul; } public String getFunction() { return function; } public void setFunction(String function) { this.function = function; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package dao; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; public class FileByte { private int bytesLength; private List<Integer> bytes; public void init() { bytes=new ArrayList<Integer>(); bytesLength=0; } //4 public final void writeInt(int v) throws IOException { if(bytes==null) { init(); } bytes.add((v >>> 24) & 0xFF); bytes.add((v >>> 16) & 0xFF); bytes.add((v >>> 8) & 0xFF); bytes.add((v >>> 0) & 0xFF); incCount(4); } //2 public final void writeShort(int v) throws IOException { if(bytes==null) { init(); } bytes.add((v >>> 8) & 0xFF); bytes.add((v >>> 0) & 0xFF); incCount(2); } //1 public final void writeByte(int v) throws IOException { if(bytes==null) { init(); } bytes.add(v); incCount(1); } private void incCount(int value) { int temp = bytesLength + value; if (temp < 0) { temp = Integer.MAX_VALUE; } bytesLength = temp; } public byte[] getByte(){ byte[] bLocalArr = new byte[bytesLength]; int i,iMax;iMax=bytes.size(); for ( i = 0; i<iMax; i++) { bLocalArr[i] = (byte) (bytes.get(i)& 0xFF); } init(); return bLocalArr; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package dao; import java.util.List; import dao.module.BtlModule0; import dao.module.BtlModule1; import dao.module.BtlModule10; import dao.module.BtlModule11; import dao.module.BtlModule12; import dao.module.BtlModule13; import dao.module.BtlModule14; import dao.module.BtlModule15; import dao.module.BtlModule16; import dao.module.BtlModule17; import dao.module.BtlModule18; import dao.module.BtlModule19; import dao.module.BtlModule2; import dao.module.BtlModule20; import dao.module.BtlModule3; import dao.module.BtlModule4; import dao.module.BtlModule5; import dao.module.BtlModule6; import dao.module.BtlModule7; import dao.module.BtlModule8; import dao.module.BtlModule9; public class BTLDAO { private BtlModule0 bm0; private List<BtlModule1> bm1; private List<BtlModule2> bm2; private List<BtlModule3> bm3; private List<BtlModule4> bm4; private List<BtlModule5> bm5; private List<BtlModule6> bm6; private List<BtlModule7> bm7; private List<BtlModule8> bm8; private List<BtlModule9> bm9; private List<BtlModule10> bm10; private List<BtlModule11> bm11; private List<BtlModule12> bm12; private List<BtlModule13> bm13; private List<BtlModule14> bm14; private List<BtlModule15> bm15; private List<BtlModule16> bm16; private List<BtlModule17> bm17; private List<BtlModule18> bm18; private List<BtlModule19> bm19; private List<BtlModule20> bm20; public BtlModule0 getBm0() { return bm0; } public List<BtlModule1> getBm1() { return bm1; } public List<BtlModule2> getBm2() { return bm2; } public List<BtlModule3> getBm3() { return bm3; } public List<BtlModule4> getBm4() { return bm4; } public List<BtlModule5> getBm5() { return bm5; } public List<BtlModule6> getBm6() { return bm6; } public List<BtlModule7> getBm7() { return bm7; } public List<BtlModule8> getBm8() { return bm8; } public List<BtlModule9> getBm9() { return bm9; } public List<BtlModule10> getBm10() { return bm10; } public List<BtlModule11> getBm11() { return bm11; } public List<BtlModule12> getBm12() { return bm12; } public List<BtlModule13> getBm13() { return bm13; } public List<BtlModule14> getBm14() { return bm14; } public List<BtlModule15> getBm15() { return bm15; } public List<BtlModule16> getBm16() { return bm16; } public List<BtlModule17> getBm17() { return bm17; } public List<BtlModule18> getBm18() { return bm18; } public List<BtlModule19> getBm19() { return bm19; } public List<BtlModule20> getBm20() { return bm20; } public void setBm0(BtlModule0 bm0) { this.bm0 = bm0; } public void setBm1(List<BtlModule1> bm1) { this.bm1 = bm1; } public void setBm2(List<BtlModule2> bm2) { this.bm2 = bm2; } public void setBm3(List<BtlModule3> bm3) { this.bm3 = bm3; } public void setBm4(List<BtlModule4> bm4) { this.bm4 = bm4; } public void setBm5(List<BtlModule5> bm5) { this.bm5 = bm5; } public void setBm6(List<BtlModule6> bm6) { this.bm6 = bm6; } public void setBm7(List<BtlModule7> bm7) { this.bm7 = bm7; } public void setBm8(List<BtlModule8> bm8) { this.bm8 = bm8; } public void setBm9(List<BtlModule9> bm9) { this.bm9 = bm9; } public void setBm10(List<BtlModule10> bm10) { this.bm10 = bm10; } public void setBm11(List<BtlModule11> bm11) { this.bm11 = bm11; } public void setBm12(List<BtlModule12> bm12) { this.bm12 = bm12; } public void setBm13(List<BtlModule13> bm13) { this.bm13 = bm13; } public void setBm14(List<BtlModule14> bm14) { this.bm14 = bm14; } public void setBm15(List<BtlModule15> bm15) { this.bm15 = bm15; } public void setBm16(List<BtlModule16> bm16) { this.bm16 = bm16; } public void setBm17(List<BtlModule17> bm17) { this.bm17 = bm17; } public void setBm18(List<BtlModule18> bm18) { this.bm18 = bm18; } public void setBm19(List<BtlModule19> bm19) { this.bm19 = bm19; } public void setBm20(List<BtlModule20> bm20) { this.bm20 = bm20; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package dao.module; public class BtlModule0 { private String bm0_1; private String bm0_2; private String bm0_3; private String bm0_4; private String bm0_5; private String bm0_6; private String bm0_7; private String bm0_8; private String bm0_9; private String bm0_10; private String bm0_11; private String bm0_12; private String bm0_13; private String bm0_14; private String bm0_15; private String bm0_16; private String bm0_17; private String bm0_18; private String bm0_19; private String bm0_20; private String bm0_21; private String bm0_22; private String bm0_23; private String bm0_24; private String bm0_25; private String bm0_26; private String bm0_27; private String bm0_28; private String bm0_29; private String bm0_30; private String bm0_31; private String bm0_32; private String bm0_33; private String bm0_34; private String bm0_35; private String bm0_36; private String bm0_37; private String bm0_38; private String bm0_39; private String bm0_40; private String bm0_41; private String bm0_42; private String bm0_43; private String bm0_44; private String bm0_45; private String bm0_46; private String bm0_47; private String bm0_48; private String bm0_49; private String bm0_50; private String bm0_51; private String bm0_52; private String bm0_53; private String bm0_54; private String bm0_55; private String bm0_56; private String bm0_57; private String bm0_58; private String bm0_59; private String bm0_60; private String bm0_61; private String bm0_62; private String bm0_63; private String bm0_64; private String bm0_65; private String bm0_66; private String bm0_67; private String bm0_68; private String bm0_69; private String bm0_70; private String bm0_71; private String bm0_72; private String bm0_73; private String bm0_74; private String bm0_75; private String bm0_76; private String bm0_77; private String bm0_78; private String bm0_79; private String bm0_80; private String bm0_81; private String bm0_82; private String bm0_83; private String bm0_84; private String bm0_85; private String bm0_86; private String bm0_87; private String bm0_88; private String bm0_89; private String bm0_90; private String bm0_91; private String bm0_92; private String bm0_93; private String bm0_94; private String bm0_95; private String bm0_96; private String bm0_97; private String bm0_98; private String bm0_99; private String bm0_100; public String getbm0_1() { return bm0_1; } public String getbm0_2() { return bm0_2; } public String getbm0_3() { return bm0_3; } public String getbm0_4() { return bm0_4; } public String getbm0_5() { return bm0_5; } public String getbm0_6() { return bm0_6; } public String getbm0_7() { return bm0_7; } public String getbm0_8() { return bm0_8; } public String getbm0_9() { return bm0_9; } public String getbm0_10() { return bm0_10; } public String getbm0_11() { return bm0_11; } public String getbm0_12() { return bm0_12; } public String getbm0_13() { return bm0_13; } public String getbm0_14() { return bm0_14; } public String getbm0_15() { return bm0_15; } public String getbm0_16() { return bm0_16; } public String getbm0_17() { return bm0_17; } public String getbm0_18() { return bm0_18; } public String getbm0_19() { return bm0_19; } public String getbm0_20() { return bm0_20; } public String getbm0_21() { return bm0_21; } public String getbm0_22() { return bm0_22; } public String getbm0_23() { return bm0_23; } public String getbm0_24() { return bm0_24; } public String getbm0_25() { return bm0_25; } public String getbm0_26() { return bm0_26; } public String getbm0_27() { return bm0_27; } public String getbm0_28() { return bm0_28; } public String getbm0_29() { return bm0_29; } public String getbm0_30() { return bm0_30; } public String getbm0_31() { return bm0_31; } public String getbm0_32() { return bm0_32; } public String getbm0_33() { return bm0_33; } public String getbm0_34() { return bm0_34; } public String getbm0_35() { return bm0_35; } public String getbm0_36() { return bm0_36; } public String getbm0_37() { return bm0_37; } public String getbm0_38() { return bm0_38; } public String getbm0_39() { return bm0_39; } public String getbm0_40() { return bm0_40; } public String getbm0_41() { return bm0_41; } public String getbm0_42() { return bm0_42; } public String getbm0_43() { return bm0_43; } public String getbm0_44() { return bm0_44; } public String getbm0_45() { return bm0_45; } public String getbm0_46() { return bm0_46; } public String getbm0_47() { return bm0_47; } public String getbm0_48() { return bm0_48; } public String getbm0_49() { return bm0_49; } public String getbm0_50() { return bm0_50; } public String getbm0_51() { return bm0_51; } public String getbm0_52() { return bm0_52; } public String getbm0_53() { return bm0_53; } public String getbm0_54() { return bm0_54; } public String getbm0_55() { return bm0_55; } public String getbm0_56() { return bm0_56; } public String getbm0_57() { return bm0_57; } public String getbm0_58() { return bm0_58; } public String getbm0_59() { return bm0_59; } public String getbm0_60() { return bm0_60; } public String getbm0_61() { return bm0_61; } public String getbm0_62() { return bm0_62; } public String getbm0_63() { return bm0_63; } public String getbm0_64() { return bm0_64; } public String getbm0_65() { return bm0_65; } public String getbm0_66() { return bm0_66; } public String getbm0_67() { return bm0_67; } public String getbm0_68() { return bm0_68; } public String getbm0_69() { return bm0_69; } public String getbm0_70() { return bm0_70; } public String getbm0_71() { return bm0_71; } public String getbm0_72() { return bm0_72; } public String getbm0_73() { return bm0_73; } public String getbm0_74() { return bm0_74; } public String getbm0_75() { return bm0_75; } public String getbm0_76() { return bm0_76; } public String getbm0_77() { return bm0_77; } public String getbm0_78() { return bm0_78; } public String getbm0_79() { return bm0_79; } public String getbm0_80() { return bm0_80; } public String getbm0_81() { return bm0_81; } public String getbm0_82() { return bm0_82; } public String getbm0_83() { return bm0_83; } public String getbm0_84() { return bm0_84; } public String getbm0_85() { return bm0_85; } public String getbm0_86() { return bm0_86; } public String getbm0_87() { return bm0_87; } public String getbm0_88() { return bm0_88; } public String getbm0_89() { return bm0_89; } public String getbm0_90() { return bm0_90; } public String getbm0_91() { return bm0_91; } public String getbm0_92() { return bm0_92; } public String getbm0_93() { return bm0_93; } public String getbm0_94() { return bm0_94; } public String getbm0_95() { return bm0_95; } public String getbm0_96() { return bm0_96; } public String getbm0_97() { return bm0_97; } public String getbm0_98() { return bm0_98; } public String getbm0_99() { return bm0_99; } public String getbm0_100() { return bm0_100; } public void setbm0_1(String bm0_1) { this.bm0_1 = bm0_1; } public void setbm0_2(String bm0_2) { this.bm0_2 = bm0_2; } public void setbm0_3(String bm0_3) { this.bm0_3 = bm0_3; } public void setbm0_4(String bm0_4) { this.bm0_4 = bm0_4; } public void setbm0_5(String bm0_5) { this.bm0_5 = bm0_5; } public void setbm0_6(String bm0_6) { this.bm0_6 = bm0_6; } public void setbm0_7(String bm0_7) { this.bm0_7 = bm0_7; } public void setbm0_8(String bm0_8) { this.bm0_8 = bm0_8; } public void setbm0_9(String bm0_9) { this.bm0_9 = bm0_9; } public void setbm0_10(String bm0_10) { this.bm0_10 = bm0_10; } public void setbm0_11(String bm0_11) { this.bm0_11 = bm0_11; } public void setbm0_12(String bm0_12) { this.bm0_12 = bm0_12; } public void setbm0_13(String bm0_13) { this.bm0_13 = bm0_13; } public void setbm0_14(String bm0_14) { this.bm0_14 = bm0_14; } public void setbm0_15(String bm0_15) { this.bm0_15 = bm0_15; } public void setbm0_16(String bm0_16) { this.bm0_16 = bm0_16; } public void setbm0_17(String bm0_17) { this.bm0_17 = bm0_17; } public void setbm0_18(String bm0_18) { this.bm0_18 = bm0_18; } public void setbm0_19(String bm0_19) { this.bm0_19 = bm0_19; } public void setbm0_20(String bm0_20) { this.bm0_20 = bm0_20; } public void setbm0_21(String bm0_21) { this.bm0_21 = bm0_21; } public void setbm0_22(String bm0_22) { this.bm0_22 = bm0_22; } public void setbm0_23(String bm0_23) { this.bm0_23 = bm0_23; } public void setbm0_24(String bm0_24) { this.bm0_24 = bm0_24; } public void setbm0_25(String bm0_25) { this.bm0_25 = bm0_25; } public void setbm0_26(String bm0_26) { this.bm0_26 = bm0_26; } public void setbm0_27(String bm0_27) { this.bm0_27 = bm0_27; } public void setbm0_28(String bm0_28) { this.bm0_28 = bm0_28; } public void setbm0_29(String bm0_29) { this.bm0_29 = bm0_29; } public void setbm0_30(String bm0_30) { this.bm0_30 = bm0_30; } public void setbm0_31(String bm0_31) { this.bm0_31 = bm0_31; } public void setbm0_32(String bm0_32) { this.bm0_32 = bm0_32; } public void setbm0_33(String bm0_33) { this.bm0_33 = bm0_33; } public void setbm0_34(String bm0_34) { this.bm0_34 = bm0_34; } public void setbm0_35(String bm0_35) { this.bm0_35 = bm0_35; } public void setbm0_36(String bm0_36) { this.bm0_36 = bm0_36; } public void setbm0_37(String bm0_37) { this.bm0_37 = bm0_37; } public void setbm0_38(String bm0_38) { this.bm0_38 = bm0_38; } public void setbm0_39(String bm0_39) { this.bm0_39 = bm0_39; } public void setbm0_40(String bm0_40) { this.bm0_40 = bm0_40; } public void setbm0_41(String bm0_41) { this.bm0_41 = bm0_41; } public void setbm0_42(String bm0_42) { this.bm0_42 = bm0_42; } public void setbm0_43(String bm0_43) { this.bm0_43 = bm0_43; } public void setbm0_44(String bm0_44) { this.bm0_44 = bm0_44; } public void setbm0_45(String bm0_45) { this.bm0_45 = bm0_45; } public void setbm0_46(String bm0_46) { this.bm0_46 = bm0_46; } public void setbm0_47(String bm0_47) { this.bm0_47 = bm0_47; } public void setbm0_48(String bm0_48) { this.bm0_48 = bm0_48; } public void setbm0_49(String bm0_49) { this.bm0_49 = bm0_49; } public void setbm0_50(String bm0_50) { this.bm0_50 = bm0_50; } public void setbm0_51(String bm0_51) { this.bm0_51 = bm0_51; } public void setbm0_52(String bm0_52) { this.bm0_52 = bm0_52; } public void setbm0_53(String bm0_53) { this.bm0_53 = bm0_53; } public void setbm0_54(String bm0_54) { this.bm0_54 = bm0_54; } public void setbm0_55(String bm0_55) { this.bm0_55 = bm0_55; } public void setbm0_56(String bm0_56) { this.bm0_56 = bm0_56; } public void setbm0_57(String bm0_57) { this.bm0_57 = bm0_57; } public void setbm0_58(String bm0_58) { this.bm0_58 = bm0_58; } public void setbm0_59(String bm0_59) { this.bm0_59 = bm0_59; } public void setbm0_60(String bm0_60) { this.bm0_60 = bm0_60; } public void setbm0_61(String bm0_61) { this.bm0_61 = bm0_61; } public void setbm0_62(String bm0_62) { this.bm0_62 = bm0_62; } public void setbm0_63(String bm0_63) { this.bm0_63 = bm0_63; } public void setbm0_64(String bm0_64) { this.bm0_64 = bm0_64; } public void setbm0_65(String bm0_65) { this.bm0_65 = bm0_65; } public void setbm0_66(String bm0_66) { this.bm0_66 = bm0_66; } public void setbm0_67(String bm0_67) { this.bm0_67 = bm0_67; } public void setbm0_68(String bm0_68) { this.bm0_68 = bm0_68; } public void setbm0_69(String bm0_69) { this.bm0_69 = bm0_69; } public void setbm0_70(String bm0_70) { this.bm0_70 = bm0_70; } public void setbm0_71(String bm0_71) { this.bm0_71 = bm0_71; } public void setbm0_72(String bm0_72) { this.bm0_72 = bm0_72; } public void setbm0_73(String bm0_73) { this.bm0_73 = bm0_73; } public void setbm0_74(String bm0_74) { this.bm0_74 = bm0_74; } public void setbm0_75(String bm0_75) { this.bm0_75 = bm0_75; } public void setbm0_76(String bm0_76) { this.bm0_76 = bm0_76; } public void setbm0_77(String bm0_77) { this.bm0_77 = bm0_77; } public void setbm0_78(String bm0_78) { this.bm0_78 = bm0_78; } public void setbm0_79(String bm0_79) { this.bm0_79 = bm0_79; } public void setbm0_80(String bm0_80) { this.bm0_80 = bm0_80; } public void setbm0_81(String bm0_81) { this.bm0_81 = bm0_81; } public void setbm0_82(String bm0_82) { this.bm0_82 = bm0_82; } public void setbm0_83(String bm0_83) { this.bm0_83 = bm0_83; } public void setbm0_84(String bm0_84) { this.bm0_84 = bm0_84; } public void setbm0_85(String bm0_85) { this.bm0_85 = bm0_85; } public void setbm0_86(String bm0_86) { this.bm0_86 = bm0_86; } public void setbm0_87(String bm0_87) { this.bm0_87 = bm0_87; } public void setbm0_88(String bm0_88) { this.bm0_88 = bm0_88; } public void setbm0_89(String bm0_89) { this.bm0_89 = bm0_89; } public void setbm0_90(String bm0_90) { this.bm0_90 = bm0_90; } public void setbm0_91(String bm0_91) { this.bm0_91 = bm0_91; } public void setbm0_92(String bm0_92) { this.bm0_92 = bm0_92; } public void setbm0_93(String bm0_93) { this.bm0_93 = bm0_93; } public void setbm0_94(String bm0_94) { this.bm0_94 = bm0_94; } public void setbm0_95(String bm0_95) { this.bm0_95 = bm0_95; } public void setbm0_96(String bm0_96) { this.bm0_96 = bm0_96; } public void setbm0_97(String bm0_97) { this.bm0_97 = bm0_97; } public void setbm0_98(String bm0_98) { this.bm0_98 = bm0_98; } public void setbm0_99(String bm0_99) { this.bm0_99 = bm0_99; } public void setbm0_100(String bm0_100) { this.bm0_100 = bm0_100; } }
BtlModule1...20与BtlModule0结构相似就不列出来了
效果如下
------------------7.3日补充---------------------
安卓中调用此反射方法顺序会变,所以需要进行一些改变
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 private static <T> Map<String, Object> cutBtl(Map rs, T item, StringBuilder buf, int bufTag, int cycleCount,String bm) { 2 Map<String, Object> rsMap = new HashMap<String, Object>(); 3 List<T> ts = new ArrayList<T>(); 4 Class clazz = item.getClass(); 5 Field[] fieldName; 6 Class clazs; 7 Field f = null; 8 int beginBufTag = bufTag; 9 int cutL, c; 10 String valueStr = null; 11 String bmId = null;//拼接的id,由于安卓的反射顺序问题,所以采用这种方式 12 for (c = 0; c < cycleCount; c++) { 13 fieldName = clazz.getDeclaredFields(); 14 try { 15 item = (T) clazz.newInstance(); 16 } catch (InstantiationException e) { 17 e.printStackTrace(); 18 } catch (IllegalAccessException e) { 19 e.printStackTrace(); 20 } 21 clazs = item.getClass(); 22 //for (n = 0; n < ruleSize; n++) { 23 for (int i = 0; i < fieldName.length; i++) { 24 bmId=bm+"_"+(i+1); 25 26 if (rs.containsKey(bmId)) { 27 // 创建实例 28 try { 29 f = clazs.getDeclaredField(bmId); 30 } catch (Exception e) { 31 e.printStackTrace(); 32 Gdx.app.log("创建实例",bmId); 33 } 34 f.setAccessible(true); 35 36 DefRule r = (DefRule) rs.get(bmId); 37 //System.out.println("B:Name1:"+r.getId()+" c:"+c+" bs:"+bufTag); 38 valueStr = getCoverStr(r, buf, bufTag); 39 /*if(c==0){ 40 Gdx.app.log("CutBtl","B:item:"+item.toString()+" id:"+r.getId()+" c:"+c+" bs:"+bufTag+"~"+(bufTag + r.getSize())+" v:"+valueStr); 41 }*/ 42 bufTag = bufTag + r.getSize(); 43 44 try { 45 f.set(item, valueStr); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 }/*else{ 50 Gdx.app.log("CutBtl警告",bmId); 51 }*/ 52 } 53 //} 54 if (item != null) { 55 ts.add(item); 56 } 57 } 58 59 if (cycleCount == 1 && beginBufTag == 0) { 60 rsMap.put("T", item); 61 } else { 62 ComUtil.removeNull(ts); 63 rsMap.put("T", ts); 64 } 65 rsMap.put("bufTag", bufTag); 66 return rsMap; 67 }