123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- 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<List<String>> descartes(List<List<String>> dimValue) {
- List<List<String>> 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<List<String>> dimValue, int index,
- List<List<String>> result, List<String> 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<String> list1 = new ArrayList<String>();
- // list1.add("普通会员");
- // list1.add("专业会员");
- // list1.add("商业会员");
- // List<String> list2 = new ArrayList<String>();
- // list2.add("1G");
- // list2.add("1T");
- // List<List<String>> dimValue = new ArrayList<List<String>>();
- // dimValue.add(list1);
- // dimValue.add(list2);
- //
- // // 递归实现笛卡尔积
- // ConvertCommonUtils s = new ConvertCommonUtils();
- // List<List<String>> res = s.descartes(dimValue);
- // System.out.println("递归实现笛卡尔乘积: 共 " + res.size() + " 个结果");
- // for (List<String> 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());
- }
- }
- }
|