- package com.help.document;
- import java.io.DataInputStream;
- public class BinFile {
- /**
- * 解析当前bin文件
- * @return 返回数据集合(包含头文件)<br>
- * 结构如下:
- * 文件头(包含身份证号,开始记录时间,文件版本号,其他)<br>
- * [0],[1],[2],[3].....[1023]<br>
- * 第一秒数据<br>
- * V6 <br>
- * [0],[1],[2],[3].....[1023]<br>
- * I<br>
- * [0],[1],[2],[3].....[1023]<br>
- * II<br>
- * [0],[1],[2],[3].....[1023]<br>
- * V2<br>
- * [0],[1],[2],[3].....[1023]<br>
- * V3<br>
- * [0],[1],[2],[3].....[1023]<br>
- * V4<br>
- * [0],[1],[2],[3].....[1023]<br>
- * V5<br>
- * [0],[1],[2],[3].....[1023]<br>
- * V1<br>
- * [0],[1],[2],[3].....[1023]<br>
- * 运动传感器数据<br>
- * [0],[1],[2],[3].....[1023]<br>
- * ....<br>
- * 第N秒数据<br>
- * @throws IOException 抛出IO异常<br>
- */
- public static List<List<String[]>> parseVoltageBinData() throws IOException{
- //声明用于保存数据总集合
- List<List<String[]>> list = new ArrayList<List<String[]>>();
- DataInputStream read = new DataInputStream(new FileInputStream(new File("E:\\\\WORK-TOOLS\\\\savejtd.bin")));
- byte []b = new byte[1024];
- int index = 1;
- List<String[]> lis = new ArrayList<String[]>();//用于保存每秒数据块集合
- while (read.read(b)!=-1) {
- String [] strs = new String[1024];//每读取1KB就重新申明数组
- if(index > 1){//读取非头文件
- for (int i = 0; i < b.length; i++) {//读取当前非头文件1KB
- strs[i] = b[i] + "";
- }
- if((index-1)%9==0){//每循环第9次则全部加入到每秒的数组中,因为从2开始所以索引需要-1
- lis.add(strs);//加入当前后并执行将当前保存此秒数据集合情况
- list.add(lis);
- lis = new ArrayList<String[]>();//9次后置空
- }else{
- lis.add(strs);//非循环到第9位直接加入当前秒数据集合
- }
- }else{//读取头文件
- for (int i = 0; i < b.length; i++) {//读取当前头文件1KB
- strs[i] = b[i] + "";
- }
- lis.add(strs);//加入当前
- list.add(lis);//加入总的集合中
- lis = new ArrayList<String[]>();
- }
- index +=1;
- }
- return list;
- }
- /**
- * 解析每秒心电数据块(包含9个块)
- * @param list
- * 每秒心电数据包含9个数据块
- * 每个块包含如下信息:
- * (标识信息:标示头,记录类型,时间戳,保留字节(0-23);数据类容(24-1023))
- */
- public static List<List<Double>> parseBinDataList(List<List<String[]>> list){
- list.remove(0);//忽略头文件
- //声明保存所有数据块数据集合
- List<List<Double>> allDataPices = new ArrayList<List<Double>>();
- for (List<String[]> list2 : list) {//循环所有数据
- for (int i=0;i<list2.size();i++) {//遍历当前一秒数据中所有块,按照文档顺序做如下判断
- if(i==0)//V6数据
- allDataPices.add(parsePice(list2.get(i)));
- if(i==1)//I数据
- allDataPices.add(parsePice(list2.get(i)));
- if(i==2)//II数据
- allDataPices.add(parsePice(list2.get(i)));
- if(i==3)//V2数据
- allDataPices.add(parsePice(list2.get(i)));
- if(i==4)//V3数据
- allDataPices.add(parsePice(list2.get(i)));
- if(i==5)//V4数据
- allDataPices.add(parsePice(list2.get(i)));
- if(i==6)//V5数据
- allDataPices.add(parsePice(list2.get(i)));
- if(i==7)//V1数据
- allDataPices.add(parsePice(list2.get(i)));
- if(i==8)//运动传感数据
- allDataPices.add(parsePice(list2.get(i)));
- }
- }
- return allDataPices;
- }
- /**
- * 解析单个心电数据块
- * @param strs 当前心电块的数据数组
- * @return 返回当前数据块内容
- * <br>
- * 每两个字节心电数值,分高低字节,每秒采样500次(1000/2)
- * 高字节计算方式:Integer.valueOf(strs[i]) + Integer.valueOf(strs[i-1]);
- * 低字节计算方式:(Integer.valueOf(strs[i]) + Integer.valueOf(strs[i-1])) + 1;
- * 2个字节得出一个电压值
- * 电压值计算方式:((高字节<<8) + 低字节 - 32768) / 2621.4;
- * 注:运动传感器数据与上不同,每四个字节一个数值,前三次代表X,Y,Z三个运动方向数据,第四个字节暂无意义
- * 每秒采样250次(1000/4)
- */
- private static List<Double> parsePice(String [] strs){
- //数据块内容
- List<Double> dataPices = new ArrayList<Double>();
- for (int i = 0; i < strs.length; i++) {
- if(i>23){//忽略前24位,只解析后1000位
- if(i>23&&i%2==0){
- //高字节
- int bigEndian = Integer.valueOf(strs[i]) + Integer.valueOf(strs[i-1]);
- //计算电压值
- Double voltageValue = ((bigEndian<<8) + (bigEndian+1) - 32768)/2621.4;
- dataPices.add(voltageValue);
- }
- }
- }
- return dataPices;
- }
- public static void main(String[] args) {
- try {
- List<List<String[]>> list = BinFile.parseVoltageBinData();
- List<List<Double>> allData = BinFile.parseBinDataList(list);
- System.out.println(allData.size());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2101201511585.html
来源: http://www.codesnippet.cn/detail/2101201511585.html