ConvertCommonUtils.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package com.fdkankan.common.util;
  2. import com.fdkankan.common.proto.Common;
  3. import com.fdkankan.common.proto.format.JsonFormat;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. @Slf4j
  9. public class ConvertCommonUtils {
  10. public static void convertVisionModelDataToTxt(String srcPath, String desPath) throws Exception {
  11. BufferedOutputStream bos = null;
  12. BufferedInputStream bis = null;
  13. try {
  14. File file = new File(srcPath);
  15. FileInputStream fis = new FileInputStream(file);
  16. Common.NavigationInfo data_NavigationInfo = Common.NavigationInfo.parseFrom(fis);
  17. String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo);
  18. ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes());
  19. bos = new BufferedOutputStream(new FileOutputStream(desPath));//设置输出路径
  20. bis = new BufferedInputStream(stream);
  21. int b = -1;
  22. while ((b = bis.read()) != -1) {
  23. bos.write(b);
  24. }
  25. //out.close();
  26. bis.close();
  27. bos.close();
  28. } catch (Exception e) {
  29. StringWriter trace = new StringWriter();
  30. e.printStackTrace(new PrintWriter(trace));
  31. log.error(trace.toString());
  32. } finally {
  33. if (bos != null) {
  34. bos.close();
  35. }
  36. if (bis != null) {
  37. bis.close();
  38. }
  39. }
  40. }
  41. public static void convertTxtToVisionModelData(String srcPath, String desPath) throws Exception {
  42. BufferedOutputStream bos = null;
  43. BufferedInputStream bis = null;
  44. try {
  45. Common.NavigationInfo.Builder builder = Common.NavigationInfo.newBuilder();
  46. String jsonFormat = readTxtFileToJson(srcPath);
  47. JsonFormat.merge(jsonFormat, builder);
  48. byte[] buf = builder.build().toByteArray();
  49. //把序列化后的数据写入本地磁盘
  50. ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  51. bos = new BufferedOutputStream(new FileOutputStream(desPath));//设置输出路径
  52. bis = new BufferedInputStream(stream);
  53. int b = -1;
  54. while ((b = bis.read()) != -1) {
  55. bos.write(b);
  56. }
  57. bis.close();
  58. bos.close();
  59. } catch (Exception e) {
  60. StringWriter trace = new StringWriter();
  61. e.printStackTrace(new PrintWriter(trace));
  62. log.error("出错",e);
  63. } finally {
  64. if (bos != null) {
  65. bos.close();
  66. }
  67. if (bis != null) {
  68. bis.close();
  69. }
  70. }
  71. }
  72. public static String readTxtFileToJson(String filePath) {
  73. try {
  74. String encoding = "UTF-8";
  75. File file = new File(filePath);
  76. if (file.isFile() && file.exists()) {
  77. InputStreamReader read = new InputStreamReader(
  78. new FileInputStream(file), encoding);
  79. BufferedReader bufferedReader = new BufferedReader(read);
  80. String lineTxt = null;
  81. String result = "";
  82. while ((lineTxt = bufferedReader.readLine()) != null) {
  83. result += lineTxt;
  84. }
  85. read.close();
  86. return result;
  87. } else {
  88. return null;
  89. }
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. return null;
  93. }
  94. }
  95. public List<List<String>> descartes(List<List<String>> dimValue) {
  96. List<List<String>> res = new ArrayList<>();
  97. if (dimValue == null || dimValue.size() == 0)
  98. return res;
  99. backtrace(dimValue, 0, res, new ArrayList<>());
  100. return res;
  101. }
  102. /**
  103. * 递归回溯法求解
  104. *
  105. * @param dimValue 原始数据集合
  106. * @param index 当前执行的集合索引
  107. * @param result 结果集合
  108. * @param curList 当前的单个结果集
  109. */
  110. private void backtrace(List<List<String>> dimValue, int index,
  111. List<List<String>> result, List<String> curList) {
  112. if (curList.size() == dimValue.size())
  113. result.add(new ArrayList<>(curList));
  114. else
  115. for (int j = 0; j < dimValue.get(index).size(); j++) {
  116. curList.add(dimValue.get(index).get(j));
  117. backtrace(dimValue, index + 1, result, curList);
  118. curList.remove(curList.size() - 1);
  119. }
  120. }
  121. public static void main(String[] args) throws Exception{
  122. // List<String> list1 = new ArrayList<String>();
  123. // list1.add("普通会员");
  124. // list1.add("专业会员");
  125. // list1.add("商业会员");
  126. // List<String> list2 = new ArrayList<String>();
  127. // list2.add("1G");
  128. // list2.add("1T");
  129. // List<List<String>> dimValue = new ArrayList<List<String>>();
  130. // dimValue.add(list1);
  131. // dimValue.add(list2);
  132. //
  133. // // 递归实现笛卡尔积
  134. // ConvertCommonUtils s = new ConvertCommonUtils();
  135. // List<List<String>> res = s.descartes(dimValue);
  136. // System.out.println("递归实现笛卡尔乘积: 共 " + res.size() + " 个结果");
  137. // for (List<String> list : res) {
  138. // for (String string : list) {
  139. // System.out.print(string + " ");
  140. // }
  141. // System.out.println();
  142. // }
  143. ConvertCommonUtils.convertTxtToVisionModelData("F:\\visiontest\\vision.txt", "F:\\visiontest\\vision.modeldata");
  144. }
  145. public static void convertTxtToVisionmodeldata(String srcpath,String despath)throws Exception
  146. {
  147. try
  148. {
  149. Common.NavigationInfo.Builder builder = Common.NavigationInfo.newBuilder();
  150. String jsonFormat = readTxtFileToJson(srcpath);
  151. JsonFormat.merge(jsonFormat, builder);
  152. byte[] buf= builder.build().toByteArray();
  153. //把序列化后的数据写入本地磁盘
  154. ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  155. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  156. BufferedInputStream bis = new BufferedInputStream(stream);
  157. int b = -1;
  158. while ((b = bis.read()) != -1) {
  159. bos.write(b);
  160. }
  161. bis.close();
  162. bos.close();
  163. }
  164. catch(Exception e)
  165. {
  166. StringWriter trace=new StringWriter();
  167. e.printStackTrace(new PrintWriter(trace));
  168. log.error(trace.toString());
  169. }
  170. }
  171. public static void convertVisionmodeldataToTxt(String srcpath,String despath)throws Exception
  172. {
  173. try
  174. {
  175. File file = new File(srcpath);
  176. FileInputStream fis=new FileInputStream(file);
  177. Common.NavigationInfo data_NavigationInfo = Common.NavigationInfo.parseFrom(fis);
  178. //PrintStream out = new PrintStream(despath);
  179. String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo);
  180. ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes());
  181. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  182. BufferedInputStream bis = new BufferedInputStream(stream);
  183. int b = -1;
  184. while ((b = bis.read()) != -1) {
  185. bos.write(b);
  186. }
  187. //out.close();
  188. bis.close();
  189. bos.close();
  190. }
  191. catch(Exception e)
  192. {
  193. StringWriter trace=new StringWriter();
  194. e.printStackTrace(new PrintWriter(trace));
  195. log.error(trace.toString());
  196. }
  197. }
  198. }