Java Bin文件时间戳读取器
我想寻求帮助,因为我有一个java代码,我使用它来读取一个加速计在log.bin文件中的存储卡上生成的数据。。。基于我在互联网上找到的git的代码可以很好地读取数据X、Y和Z。但是他没有读取时间,这就是“时间戳”,我不知道如何实现它 文件结构在本git中描述如下: 使用的代码是:Java Bin文件时间戳读取器,java,binary,binaries,Java,Binary,Binaries,我想寻求帮助,因为我有一个java代码,我使用它来读取一个加速计在log.bin文件中的存储卡上生成的数据。。。基于我在互联网上找到的git的代码可以很好地读取数据X、Y和Z。但是他没有读取时间,这就是“时间戳”,我不知道如何实现它 文件结构在本git中描述如下: 使用的代码是: import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedWriter; impo
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ConvertGT3x {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("usage: java ZipAccess zipfile");
}
int value = 0xF91;
value = (value & 0x800) > 0 ? (0x800 - (value & 0x7FF)) * -1 : value;
double v = (((double)value * 2.93f) / 1000.0);
log("Value: " + value + " v: " + v);
// Extract the activity file
if (unzipActivityFile("C:\\Users\\leona\\Downloads\\origin\\MOS2D21160307.gt3x") == true) {
convertFile("log.bin");
}
}
private static boolean convertFile(String filename) {
filename = "log.bin";
log("Reading in binary file named : " + filename);
File file = new File("C:\\Users\\leona\\Downloads\\origin\\MOS2D21160307\\log.bin");
log("File size: " + file.length());
//What should be a good buffer length????
byte[] result = new byte[4096];
short[] xyzBuffer = new short[9];
int[] activityXYZ = new int[3];
try {
InputStream input = null;
try {
try {
//open text file for writing
FileWriter fstream = new FileWriter("C:\\Users\\leona\\Downloads\\origin\\MOS2D21160307\\data.txt");
BufferedWriter out = new BufferedWriter(fstream);
int totalBytesRead = 0;
input = new BufferedInputStream(new FileInputStream(file));
while (totalBytesRead < file.length()) {
int bytesRemaining = (int)file.length() - totalBytesRead;
// input.read() returns -1, 0, or more :
//Standard read os 4095 bytes
//dividable with 9 to fit the 9 byte data window of the gt3x data file
int bytesRead = input.read(result, 0, 4095);
if (bytesRead > 0) {
//Data has been read
//convert to text
//int startBlock = 0;
int blockLength = 9;
for (int i = 0; i < bytesRead; i+=blockLength) {
System.out.println(bytesRead);
//converting to unsigned byte
for (int n = 0;n<9;n++)
xyzBuffer[n] = (short) (result[i+n] & 0xff);
//converting to doubles from 12 bit
double x = convert2Complement2float((short)(xyzBuffer[0] << 4 | ((xyzBuffer[1] & 0xf0) >> 4)));
double y = convert2Complement2float( (short) (((xyzBuffer[1] & 0x0f) << 8) | xyzBuffer[2]));
double z = convert2Complement2float( (short) ((xyzBuffer[3] << 4) | ((xyzBuffer[4] & 0xf0) >> 4)));
//write x y z to text file
String cvsStr = String.format("%2.3f,%2.3f,%2.3f%n", x,y,z);
out.write(cvsStr);
x = convert2Complement2float( (short) (((xyzBuffer[4] & 0x0f) << 8) | xyzBuffer[5]));
y = convert2Complement2float((short)(xyzBuffer[6] << 4 | ((xyzBuffer[7] & 0xf0) >> 4)));
z = convert2Complement2float( (short) (((xyzBuffer[7] & 0x0f) << 8) | xyzBuffer[8]));
//Write x y z to text file
log("X: "+x+" Y: "+y+" Z: "+z);
cvsStr = String.format("%2.3f,%2.3f,%2.3f%n", x,y,z);
out.write(cvsStr);
}
totalBytesRead = totalBytesRead + bytesRead;
}
}
//Close text file
out.close();
log("Num bytes read: " + totalBytesRead);
} catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
/*
* the above style is a bit tricky: it places bytes into the
* 'result' array; 'result' is an output parameter; the while
* loop usually has a single iteration only.
*/
} finally {
log("Closing input stream.");
input.close();
}
} catch (FileNotFoundException ex) {
log("File not found.");
} catch (IOException ex) {
log(ex.getMessage());
}
return true;
}
private static void log(String text) {
System.out.println(text);
}
private static double convert2Complement2float(int ui) {
//ui = (ui & 0x800) ? ((0x7FF - (ui & 0x7FF)) * -1) : ui;
if ((ui & 0x800) > 0) {
return ((0x7FF - (ui & 0x7FF)) * -1) * 2.93 / 1000.0;
}
return (double)(ui * 2.93 / 1000.0);
}
private static boolean unzipActivityFile(String zipname) {
try {
// Take the filename from the input arguments
// String zipname = args[0];
FileInputStream fis = new FileInputStream(zipname);
//
// Creating input stream that also maintains the checksum of the
// data which later can be used to validate data integrity.
//
CheckedInputStream checksum = new CheckedInputStream(fis,
new Adler32());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(
checksum));
ZipEntry entry;
//
// Read each entry from the ZipInputStream until no more entry found
// indicated by a null return value of the getNextEntry() method.
//
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Unzipping: " + entry.getName());
if (! (entry.getName().equalsIgnoreCase("activity.bin") == true ||
entry.getName().equals("info.txt") == true) )
continue;
int size;
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(entry.getName());
BufferedOutputStream bos = new BufferedOutputStream(fos,
buffer.length);
while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
}
zis.close();
fis.close();
//
// Print out the checksum value
//
System.out.println("Checksum = "
+ checksum.getChecksum().getValue());
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}```
would anyone know how to add an implement to read this timestamp from the file?
import java.io.BufferedInputStream;
导入java.io.BufferedOutputStream;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.zip.Adler32;
导入java.util.zip.CheckedInputStream;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipInputStream;
公共类转换器GT3X{
公共静态void main(字符串[]args){
如果(args.length!=1){
System.err.println(“用法:javazipaccesszipfile”);
}
int值=0xF91;
value=(value&0x800)>0?(0x800-(value&0x7FF))*-1:值;
双v=((双)值*2.93f)/1000.0);
日志(“值:+Value+”v:+v);
//提取活动文件
if(unzipActivityFile(“C:\\Users\\leona\\Downloads\\origin\\MOS2D21160307.gt3x”)==true){
转换文件(“log.bin”);
}
}
私有静态布尔转换文件(字符串文件名){
filename=“log.bin”;
日志(“读取名为:“+filename”的二进制文件);
File File=新文件(“C:\\Users\\leona\\Downloads\\origin\\MOS2D21160307\\log.bin”);
日志(“文件大小:+File.length());
//好的缓冲区长度应该是多少????
字节[]结果=新字节[4096];
short[]xyzBuffer=新的short[9];
int[]activityXYZ=新的int[3];
试一试{
InputStream输入=null;
试一试{
试一试{
//打开文本文件进行写入
FileWriter fstream=newFileWriter(“C:\\Users\\leona\\Downloads\\origin\\MOS2D21160307\\data.txt”);
BufferedWriter out=新的BufferedWriter(fstream);
int totalBytesRead=0;
输入=新的BufferedInputStream(新文件输入流(文件));
while(totalBytesRead0){
//数据已被读取
//转换为文本
//int startBlock=0;
int blockLength=9;
for(int i=0;i