package com.fdkankan.common.util; import com.fdkankan.common.proto.Common; import com.fdkankan.common.proto.format.JsonFormat; import lombok.extern.slf4j.Slf4j; import java.io.*; import java.util.ArrayList; import java.util.List; @Slf4j public class ConvertCommonUtils { public static void convertVisionModelDataToTxt(String srcPath, String desPath) throws Exception { BufferedOutputStream bos = null; BufferedInputStream bis = null; try { File file = new File(srcPath); FileInputStream fis = new FileInputStream(file); Common.NavigationInfo data_NavigationInfo = Common.NavigationInfo.parseFrom(fis); String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo); ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes()); bos = new BufferedOutputStream(new FileOutputStream(desPath));//设置输出路径 bis = new BufferedInputStream(stream); int b = -1; while ((b = bis.read()) != -1) { bos.write(b); } //out.close(); bis.close(); bos.close(); } catch (Exception e) { StringWriter trace = new StringWriter(); e.printStackTrace(new PrintWriter(trace)); log.error(trace.toString()); } finally { if (bos != null) { bos.close(); } if (bis != null) { bis.close(); } } } public static void convertTxtToVisionModelData(String srcPath, String desPath) throws Exception { BufferedOutputStream bos = null; BufferedInputStream bis = null; try { Common.NavigationInfo.Builder builder = Common.NavigationInfo.newBuilder(); String jsonFormat = readTxtFileToJson(srcPath); JsonFormat.merge(jsonFormat, builder); byte[] buf = builder.build().toByteArray(); //把序列化后的数据写入本地磁盘 ByteArrayInputStream stream = new ByteArrayInputStream(buf); bos = new BufferedOutputStream(new FileOutputStream(desPath));//设置输出路径 bis = new BufferedInputStream(stream); int b = -1; while ((b = bis.read()) != -1) { bos.write(b); } bis.close(); bos.close(); } catch (Exception e) { StringWriter trace = new StringWriter(); e.printStackTrace(new PrintWriter(trace)); log.error("出错",e); } finally { if (bos != null) { bos.close(); } if (bis != null) { bis.close(); } } } public static String readTxtFileToJson(String filePath) { try { String encoding = "UTF-8"; File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; String result = ""; while ((lineTxt = bufferedReader.readLine()) != null) { result += lineTxt; } read.close(); return result; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } public List> descartes(List> dimValue) { List> res = new ArrayList<>(); if (dimValue == null || dimValue.size() == 0) return res; backtrace(dimValue, 0, res, new ArrayList<>()); return res; } /** * 递归回溯法求解 * * @param dimValue 原始数据集合 * @param index 当前执行的集合索引 * @param result 结果集合 * @param curList 当前的单个结果集 */ private void backtrace(List> dimValue, int index, List> result, List curList) { if (curList.size() == dimValue.size()) result.add(new ArrayList<>(curList)); else for (int j = 0; j < dimValue.get(index).size(); j++) { curList.add(dimValue.get(index).get(j)); backtrace(dimValue, index + 1, result, curList); curList.remove(curList.size() - 1); } } public static void main(String[] args) throws Exception{ // List list1 = new ArrayList(); // list1.add("普通会员"); // list1.add("专业会员"); // list1.add("商业会员"); // List list2 = new ArrayList(); // list2.add("1G"); // list2.add("1T"); // List> dimValue = new ArrayList>(); // dimValue.add(list1); // dimValue.add(list2); // // // 递归实现笛卡尔积 // ConvertCommonUtils s = new ConvertCommonUtils(); // List> res = s.descartes(dimValue); // System.out.println("递归实现笛卡尔乘积: 共 " + res.size() + " 个结果"); // for (List list : res) { // for (String string : list) { // System.out.print(string + " "); // } // System.out.println(); // } ConvertCommonUtils.convertTxtToVisionModelData("F:\\visiontest\\vision.txt", "F:\\visiontest\\vision.modeldata"); } public static void convertTxtToVisionmodeldata(String srcpath,String despath)throws Exception { try { Common.NavigationInfo.Builder builder = Common.NavigationInfo.newBuilder(); String jsonFormat = readTxtFileToJson(srcpath); JsonFormat.merge(jsonFormat, builder); byte[] buf= builder.build().toByteArray(); //把序列化后的数据写入本地磁盘 ByteArrayInputStream stream = new ByteArrayInputStream(buf); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径 BufferedInputStream bis = new BufferedInputStream(stream); int b = -1; while ((b = bis.read()) != -1) { bos.write(b); } bis.close(); bos.close(); } catch(Exception e) { StringWriter trace=new StringWriter(); e.printStackTrace(new PrintWriter(trace)); log.error(trace.toString()); } } public static void convertVisionmodeldataToTxt(String srcpath,String despath)throws Exception { try { File file = new File(srcpath); FileInputStream fis=new FileInputStream(file); Common.NavigationInfo data_NavigationInfo = Common.NavigationInfo.parseFrom(fis); //PrintStream out = new PrintStream(despath); String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo); ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径 BufferedInputStream bis = new BufferedInputStream(stream); int b = -1; while ((b = bis.read()) != -1) { bos.write(b); } //out.close(); bis.close(); bos.close(); } catch(Exception e) { StringWriter trace=new StringWriter(); e.printStackTrace(new PrintWriter(trace)); log.error(trace.toString()); } } }