java操作二进制_Java读取二进制文件

package com.ccssoft.bin;

import java.io.*;

public class ParseBinary

{

public static void main(String[] args) throws Exception

{

String source = "H://NATLOG_280000069469_20160919211200_0000.DAT";

String destination = "H://NATLOG_280000069469_20160919211200_0000.DAT.txt";

int bufferSize = 4096; // 设置缓冲区大小

byte buffer[] = new byte[bufferSize]; // 缓冲区字节数组

File sourceFile = new File(source);

InputStream fis = new FileInputStream(sourceFile);

BufferedInputStream bis = new BufferedInputStream(fis, bufferSize);

OutputStream fos = new FileOutputStream(destination);

BufferedOutputStream bos = new BufferedOutputStream(fos, bufferSize);

long fileSize = sourceFile.length(); // 文件总字节数

int haveRead = 0; // 已读取字节数

int readSize = -1; // 记录每次实际读取字节数

while (null != bis && (readSize = bis.read(buffer)) != -1)

{

haveRead += readSize;

bos.write(buffer, 0, readSize);

System.out.println("已经复制: " + haveRead + " Byte 完成" + haveRead * 100 / fileSize + "% 单次读取:" + readSize + " Byte");

}

bos.flush();

bos.close();

bis.close();

System.out.println("复制完成: " + haveRead);

}

}

已经复制: 4096 Byte 完成5% 单次读取:4096 Byte

已经复制: 8192 Byte 完成11% 单次读取:4096 Byte

已经复制: 12288 Byte 完成17% 单次读取:4096 Byte

已经复制: 16384 Byte 完成23% 单次读取:4096 Byte

已经复制: 20480 Byte 完成28% 单次读取:4096 Byte

已经复制: 24576 Byte 完成34% 单次读取:4096 Byte

已经复制: 28672 Byte 完成40% 单次读取:4096 Byte

已经复制: 32768 Byte 完成46% 单次读取:4096 Byte

已经复制: 36864 Byte 完成52% 单次读取:4096 Byte

已经复制: 40960 Byte 完成57% 单次读取:4096 Byte

已经复制: 45056 Byte 完成63% 单次读取:4096 Byte

已经复制: 49152 Byte 完成69% 单次读取:4096 Byte

已经复制: 53248 Byte 完成75% 单次读取:4096 Byte

已经复制: 57344 Byte 完成81% 单次读取:4096 Byte

已经复制: 61440 Byte 完成86% 单次读取:4096 Byte

已经复制: 65536 Byte 完成92% 单次读取:4096 Byte

已经复制: 69632 Byte 完成98% 单次读取:4096 Byte

已经复制: 70742 Byte 完成100% 单次读取:1110 Byte

复制完成: 70742

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值