java 二进制 保存文件怎么打开,如何在Java中将二进制数据输出到文件?

博客内容讲述了作者尝试将仅包含浮点数的数据进行量化,转换为0到65535之间的整数,以节省存储空间。作者目前能将数据以文本格式写入文件,但需要以二进制形式输出。解决方案提供了使用`DataOutputStream`将整数写入二进制文件的示例代码。
摘要由CSDN通过智能技术生成

I'm trying to write data to a file in binary format for compression. The data consists entirely of floating points so I decided to quantize the data to an intergers between 0 and 65535 so the data can be written as two bit unsigned integers and ultimately save space. However, I need to output that quantized data to a file in binary instead of human-readable Ascii.

At the moment this is what I'm doing

@param outputFile the file containing the already quantized data as strings in a .txt file

public void generateBinaryRioFile(String materialLibrary,

String outputFile, String group, String mtlAux) {

try {

// Create file

FileWriter fileStream = new FileWriter(outputFile);

try {

BufferedReader br = new BufferedReader(new FileReader(new File(

"idx.txt")));

while ((line = br.readLine()) != null) {

writer.write(line + "\n");

}

try {

br.close();

} catch (FileNotFoundException e) {

e.getMessage();

} catch (IOException e) {

e.printStackTrace();

} BufferedWriter writer = new BufferedWriter(fileStream);

However that writes to the file as a human readable string. I need it to be written as binary data. How does one go about doing this in Java?

解决方案

Maybe this fragment will help.

int i = 42;

DataOutputStream os = new DataOutputStream(new FileOutputStream("C:\\binout.dat"));

os.writeInt(i);

os.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值