java 解析二进制文件保存为txt文件
本文包含二进制解析,遍历文件夹下的文件,生成对应得文件夹以及文件,日期转换的知识点

import java.io.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.*;/*
SL 二进制文件解析*/
public class ReadSL {//阻塞队列线程安全private static BlockingQueue<File> queue = new LinkedBlockingQueue<File>(10000);//线程池private static ExecutorService threadPool = Executors.newFixedThreadPool(2);public static void main(String[] args) throws Exception {//监控线程threadPool.execute(new Runnable() {public void run() {while (true) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}int size = queue.size();System.out.println("还有:" + size + "个元素");threadPool.shutdown();}}});for (int i = 1; i <= 1; i++) {threadPool.execute(new Runnable() {public void run() {while (true) {try {//从队列中获取文件File file = queue.take();parseText(file);} catch (Exception e) {e.printStackTrace();}}}});}traverseFolder2("D:\\test");}//字节数组转单精度public static float  bytetofloat(byte[] b) {int accum = 0;accum = accum | (b[0] & 0xff) << 0;accum = accum | (b[1] & 0xff) << 8;accum = accum | (b[2] & 0xff) << 16;accum = accum | (b[3] & 0xff) << 24;float accumf= Float.intBitsToFloat(accum);BigDecimal bd = new BigDecimal(accumf);accumf= bd.setScale(6,BigDecimal.ROUND_HALF_UP).floatValue();return accumf;}//字节数组转intpublic static int bytetoInt(byte[] b) {int jd = 0;int jd0 = b[0] & 0xff;// 最低位int jd1 = b[1] & 0xff;int jd2 = b[2] & 0xff;int jd3 = b[3] & 0xff;jd3 <<= 24;jd2 <<= 16;jd1 <<= 8;jd = jd0 | jd1 | jd2 | jd3;return jd;}//时间转换public static String getDate(int t) throws ParseException {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String historyTime = "1970-01-01 08:00:00";Date date = dateFormat.parse(historyTime);Date newDate = addSeconds(date, t);return dateFormat.format(newDate);}private static Date addSeconds(Date date, int seconds) {Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.add(Calendar.SECOND, seconds);return calendar.getTime();}public static void traverseFolder2(String path) throws Exception {//空文件夹处理File file = new File(path);if (file.exists()) {File[] files = file.listFiles();if (files.length == 0) {return;} else {for (File file2 : files) {if (file2.isDirectory()) {traverseFolder2(file2.getAbsolutePath());} else {//将文件放入阻塞队列中queue.put(file2);}}}} else {System.out.println("文件不存在!");}}public static void parseText(File file) throws Exception {String result0 = null;//文件的路径File infile = new File(file.getAbsolutePath());//文件名称String name = infile.getName();//路径String absolutePath = infile.getAbsolutePath();//输入流InputStream bis = new FileInputStream(infile);//输出路径String outPath = "D:\\parseData";//切割文件路径String[] split = absolutePath.split("\\\\");byte[] b = new byte[1022];int len = 0;int count = 0;//txt文件暂不处理if (infile.getName().substring(name.length() - 4, name.length()).equals(".txt")) {return;} else {String time = "";long historyMillons = 0;String listStr = "";String listStr2="";ArrayList<String> list = new ArrayList<String>();//时间long historyTimeMillins=0;String path1 = "\\" + split[1] + "\\" + split[2] + "\\" + split[3] + "\\" + split[4] + "\\" + split[5] + "\\";outPath = outPath + path1 + infile.getName().substring(0, name.length() - 3) + ".txt";//创建文件createFiles(outPath);//输出流,追加写入文件BufferedWriter bw = new BufferedWriter(new FileWriter(outPath,true));//保留小数点后六位数字DecimalFormat format = new DecimalFormat("0.000000");while ((len = bis.read(b)) != -1) {count++;if (count == 1) {//版本号byte[] b1 = { b[1], b[2], b[3], b[4], b[5], b[6],b[7],b[8]};//传感器类型简写byte[] b2 = {b[9], b[10]};//传感器类型全称byte[] b3 = {b[12], b[13], b[14], b[15], b[16], b[17], b[18], b[19]};//时间byte[] b4 = {b[20], b[21], b[22], b[23]};//传感器编号byte[] b5 = {b[25], b[26], b[27], b[28], b[29], b[30], b[31], b[32]};String bbh = new String(b1, "UTF-8");//System.out.println("版本号:" + bbh);String jianXie = new String(b2, "UTF-8");String fullName = new String(b3, "GBK");//时间操作  字节数组转换long类型long s0 = b4[0] & 0xff;// 最低位long s1 = b4[1] & 0xff;long s2 = b4[2] & 0xff;long s3 = b4[3] & 0xff;s1 <<= 8;s2 <<= 16;s3 <<= 24;historyMillons = s0 | s1 | s2 | s3;time = getDate((int) historyMillons);// System.out.println("采集时间:" + time);String bianHao = new String(b5, "utf-8");//  System.out.println("传感器编号:" + bianHao);//频率 单精度byte[] b6 = {b[33], b[34], b[35], b[36]};float pinLv = bytetofloat(b6);//采样精度 32位整型byte[] b7 = {b[37], b[38], b[39], b[40]};int jingDu = bytetoInt(b7);//放大倍数byte[] b8 = {b[41], b[42], b[43], b[44]};int beiShu = bytetoInt(b8);//传感器灵敏度byte[] b9 = {b[45], b[46], b[47], b[48]};float minGanDu = bytetofloat(b9);//文件头信息结束标志 $//  byte[] b11 = {b[49]};//System.out.println(new String(b11));//文件头信息result0 = bbh + "\t" + jianXie + "\t" + fullName + "\t" + time + "\t" + bianHao + "\t" + pinLv + "\t" + jingDu + "\t" + beiShu + "\t" + minGanDu + "\t";//System.out.println(b.length);//真正数据部分for (int j = 50; j <= len - 4; j += 4) {byte[] b12 = {b[j], b[j + 1], b[j + 2], b[j + 3]};float cjsj = bytetofloat(b12);String flToStr = String.valueOf(cjsj);//取消科学计数法flToStr= format.format(cjsj);historyTimeMillins=historyMillons * 1000 + 50 * ((j - 50) / 4);flToStr =  historyTimeMillins+ " " + flToStr;list.add(flToStr);}b=new byte[102400*10];}else {for (int i = 0; i < len - 3; i+=4) {byte[] b13 = {b[i], b[i + 1], b[i + 2], b[i + 3]};float cjsj = bytetofloat(b13);//取消科学计数法String format1 = format.format(cjsj);format1= historyTimeMillins+ 50 * (i / 4) + " " + format1;//System.out.println("采集数据:" + cjsj);list.add(format1);//listStr2 += flToStr + "\r\n";}}}//文件追加写入bw.write(result0+"\r\n");//System.out.println(list.size());for (int i =0;i<list.size();i++){//换行写入bw.write(list.get(i)+"\r\n");}bw.flush();bis.close();bw.close();}}public static void createFiles (String outPath) throws IOException {File outFile = new File(outPath);//输出路径File parentFile = outFile.getParentFile();if (!parentFile.exists()) {parentFile.mkdirs();}outFile.createNewFile();}public static void createDir (String outPath){File outFile = new File(outPath);//输出路径File parentFile = outFile.getParentFile();if (!parentFile.exists()) {parentFile.mkdirs();}outFile.mkdirs();}}

java 解析二进制文件保存为txt文件相关推荐

  1. 如何将一组列表(三个以上,数值类型不一)保存为txt文件

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 郡邑浮前浦,波澜动远空. 大家好, ...

  2. python将输出保存为txt_Python3将数据保存为txt文件的方法

    Python3将数据保存为txt文件的方法,具体内容如下所示:f = open("data/model_Weight.txt",'a') #若文件不存在,系统自动创建.'a'表示可 ...

  3. Java解析魔兽争霸3录像W3G文件(五):Action和APM计算

    在游戏进行中,玩家会进行各种操作,例如编队.移动.技能.造建筑等,这些操作就是Action.APM(Actions Per Minute),表示每分钟的操作次数,APM可以很好的反映玩家的手速和实力, ...

  4. android 数据存储怎么保存图片_文章要保存为TXT文件,其中的图片要怎么办?Python帮你解决...

    前言 用 python 爬取你喜欢的 CSDN 的原创文章,保存为TXT文件,不仅查看不方便,而且还无法保存文章中的代码和图片. 今天教你制作成 PDF 慢慢看.万一作者的突然把号给删了,也会保存备份 ...

  5. Py库下载:Dos内一条命令快速全部下载(先列出要下载的库名称保存为txt文件)

    Py库下载:Dos内一条命令快速全部下载(先列出要下载的库名称保存为txt文件) 目录 输出结果 设计过程 输出结果 设计过程 第一步:先列出要下载库的名称,然后保存到txt文档内,一行一个库名 第二 ...

  6. 保存MATLAB中间变量值的方法:保存为txt文件或者mat文件

    1.直接保存为TXT文件: 具体的命令是:用save *.txt -ascii x (x为变量: *.txt为文件名,该文件存储于当前工作目录下,再打开就可以 打开后,数据有可能是以指数形式保存的.) ...

  7. 【Matlab】Matlab将mat文件保存为txt文件

    Matlab将mat文件保存为txt文件 1. 使用dlmwrite函数实现 将矩阵 M 写入文件 'myFile.txt',用制表位字符分隔并使用 3 位数精度. dlmwrite('myFile. ...

  8. r语言写九九乘法表并保存为txt文件

    r语言写九九乘法表并保存为txt文件 代码 for(i in 1:9){for(j in 1:i){cat(j,"x",i,"=",i*j,'\t',file= ...

  9. java将多个文件写入到一个文件流中再有规律的取出_编写java程序,往一个txt文件里写入学生的基本信息,然后再读出这些信息并打印出来,最后把该文件拷贝到指...

    满意答案 azonyb 2014.02.18 采纳率:59%    等级:12 已帮助:13223人 import java.io.BufferedInputStream; import java.i ...

  10. 深度学习制作自己的数据集—为数据集打上标签保存为txt文件,并进行划分和加载数据集

    目录 0 前言 1 为图片数据集打上标签并保存为txt文件 2 将txt文件中的图片标签数据集随机划分为训练集和测试集 3 加载txt文件中的图片标签数据集 0 前言 目前是被封控的第四天了,只能呆在 ...

最新文章

  1. gitlab中文_ASP.NET实战010:Visual Studio使用GitLab详解
  2. Linux查看ip的命令详解
  3. Android Fragment嵌套导致的bug
  4. First Grid 枚举(100)
  5. linux安装完怎么分区,Linux系统安装模式下硬盘分区怎么做?
  6. android mqtt详解_Android mqtt入门 Android studio(转)
  7. Microsoft BizTalk ESB Toolkit 2.0
  8. 非常时期,手机不能没电不能离身啊
  9. Bootstrap列表组禁用和活动状态
  10. 系统的性能瓶颈,排查该从哪些方面入手,如何定位?
  11. POM思想__多个页面时进行的处理
  12. [译]Javascript中的mutators
  13. 表锁 行锁 页锁 是什么区别
  14. iOS开发:报错The sandbox is not in sync with the Podfile.lock. Run ‘pod install‘ …的解决方法
  15. TMQ:定时消息队列原理与实现
  16. LabVIEW 2021 工具包
  17. 有意思的atoll函数
  18. 关于谷歌浏览器Google Chrome 打开所有网页都显示“无标题”的解决办法。
  19. 【论文精读】:Global Matching with Overlapping Attention for Optical Flow Estimation
  20. 什么是百度权重 百度指数词

热门文章

  1. 【电子技术实验理论】语音放大电路
  2. lammps教程:单原子应力计算及应力云图绘制方法
  3. SD卡格式化咋办?数据恢复看这里!
  4. H264视频编码原理基础分析
  5. 如何使用计算机中的导出,解决方案:如何使用Canon 2525i复印机将文档扫描到计算机中并生成PDF格式?...
  6. Cadence Allegro 高亮功能的使用技巧图文教程
  7. Excel数据透视表学习
  8. stc12c5a60s2制作4*4*4,光立方c语言程序怎么写,基于STC12c5a60s2单片机的3D显示屏设计...
  9. PLC编程器的功能有什么功能?
  10. JSP程序设计课后习题答案