数据分包--测试发布

2021年03月18日,开始慢慢记录学习过程

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class TestDat{
  public static void main(String[] args) throws IOException {
	long startTime = System.currentTimeMillis();
	int sum = 0;
    File file = new File("./temp.dat");
    InputStream fis = new FileInputStream(file);
    OutputStream ois05 = new FileOutputStream(new File("./1.dat"));
    OutputStream ois15 = new FileOutputStream(new File("./2.dat"));
    OutputStream ois33 = new FileOutputStream(new File("./3.dat"));
    OutputStream ois3F = new FileOutputStream(new File("./4.dat"));
    OutputStream ois00 = new FileOutputStream(new File("./5.dat"));
    Scanner scanner = new Scanner(System.in);
    ByteArrayOutputStream bos;
    byte[] buf = new byte[1024]; 
    int readNum;
    System.out.println("开始分包:......");
    while ((readNum = fis.read(buf)) != -1) {
      bos = new ByteArrayOutputStream();
      bos.write(buf, 0, readNum);
      StringBuffer sb = new StringBuffer();
      String s = "";
      for (int i = 0; i < buf.length; i++) {
        String hex = Integer.toHexString(buf[i] & 0xFF);
        if (hex.length() == 1) {
          hex = String.valueOf('0') + hex;
        }
        sb.append(hex.toUpperCase());
      } 
      
      s = sb.toString();
      String s1 = s.substring(10, 12);
      if (s1.equals("05")) {
        byte[] bbb = Hexstr2(s);
        ois05.write(bbb); //continue;
      }else if(s1.equals("15")) {
        byte[] bbb = Hexstr2(s);
        ois15.write(bbb); //continue;
      }else if (s1.equals("33")) {
        byte[] bbb = Hexstr2(s);
        ois33.write(bbb); //continue;
      }else if (s1.equals("3F")) {
        byte[] bbb = Hexstr2(s);
        ois3F.write(bbb); //continue;
      }else{ 
    	byte[] bbb = Hexstr2(s);
        ois00.write(bbb);
      }
      sum++;
    } 
    long endTime = System.currentTimeMillis();
    System.out.println("所需时长:"+(endTime-startTime)+"毫秒");
    System.out.println("总共有"+sum+"帧");
    if((readNum = fis.read(buf)) == -1){
    	System.out.print("分包完成,按1退出:");
        int num = scanner.nextInt();
        System.out.println(num);
        scanner.close();
        ois00.close();
        ois3F.close();
        ois33.close();
        ois15.close();
        ois05.close();
        fis.close();
    }  
  }
  
  public static byte[] Hexstr2(String str) {
    if (str.length() < 1) {
      return null;
    }
    byte[] result = new byte[str.length() / 2];
    for (int i = 0; i < str.length() / 2; i++) {
      int high = Integer.parseInt(str.substring(i * 2, i * 2 + 1), 16);
      int low = Integer.parseInt(str.substring(i * 2 + 1, i * 2 + 2), 16);
      result[i] = (byte)(high * 16 + low);
    } 
    return result;
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值