ConvertUtils.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package com.fdkankan.common.util;
  2. import com.fdkankan.common.proto.Visionmodeldata;
  3. import com.fdkankan.common.proto.format.JsonFormat;
  4. import lombok.extern.log4j.Log4j2;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. @Log4j2
  9. public class ConvertUtils {
  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. Visionmodeldata.NavigationInfo data_NavigationInfo = Visionmodeldata.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. Visionmodeldata.NavigationInfo.Builder builder = Visionmodeldata.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(trace.toString());
  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) {
  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. ConvertUtils s = new ConvertUtils();
  134. List<List<String>> res = s.descartes(dimValue);
  135. System.out.println("递归实现笛卡尔乘积: 共 " + res.size() + " 个结果");
  136. for (List<String> list : res) {
  137. for (String string : list) {
  138. System.out.print(string + " ");
  139. }
  140. System.out.println();
  141. }
  142. }
  143. public static void convertTxtToVisionmodeldata(String srcpath,String despath)throws Exception
  144. {
  145. try
  146. {
  147. Visionmodeldata.NavigationInfo.Builder builder = Visionmodeldata.NavigationInfo.newBuilder();
  148. String jsonFormat = readTxtFileToJson(srcpath);
  149. JsonFormat.merge(jsonFormat, builder);
  150. byte[] buf= builder.build().toByteArray();
  151. //把序列化后的数据写入本地磁盘
  152. ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  153. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  154. BufferedInputStream bis = new BufferedInputStream(stream);
  155. int b = -1;
  156. while ((b = bis.read()) != -1) {
  157. bos.write(b);
  158. }
  159. bis.close();
  160. bos.close();
  161. }
  162. catch(Exception e)
  163. {
  164. StringWriter trace=new StringWriter();
  165. e.printStackTrace(new PrintWriter(trace));
  166. log.error(trace.toString());
  167. }
  168. }
  169. public static void convertVisionmodeldataToTxt(String srcpath,String despath)throws Exception
  170. {
  171. try
  172. {
  173. File file = new File(srcpath);
  174. FileInputStream fis=new FileInputStream(file);
  175. Visionmodeldata.NavigationInfo data_NavigationInfo = Visionmodeldata.NavigationInfo.parseFrom(fis);
  176. //PrintStream out = new PrintStream(despath);
  177. String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo);
  178. ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes());
  179. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
  180. BufferedInputStream bis = new BufferedInputStream(stream);
  181. int b = -1;
  182. while ((b = bis.read()) != -1) {
  183. bos.write(b);
  184. }
  185. //out.close();
  186. bis.close();
  187. bos.close();
  188. }
  189. catch(Exception e)
  190. {
  191. StringWriter trace=new StringWriter();
  192. e.printStackTrace(new PrintWriter(trace));
  193. log.error(trace.toString());
  194. }
  195. }
  196. }