简体   繁体   English

用Java读取二进制文件

[英]Reading binary file in Java

Let's say I want to read a binary file which contains three of data types char, int and double (However,this binary file can contain any number of these three data types in any order.). 假设我要读取一个二进制文件,其中包含char,int和double三种数据类型(但是,此二进制文件可以按任意顺序包含这三种数据类型中的任意一种。) And then I want to store three of these data types to char array list, double array list and integer array list. 然后,我要将这些数据类型中的三种存储到char数组列表,double数组列表和integer数组列表中。 Anyone could be able to help? 任何人都可以提供帮助吗? I really appreciate it. 我真的很感激。

Your file needs some kind of map on how to read it. 您的文件需要某种有关如何阅读的地图。 There is no magic function that can distinguish between lets say 4 char and 1 int. 没有可以区分例如4个字符和1个整数的魔术函数。

You need to open the file, read the map then read the file accordingly 您需要打开文件,阅读地图,然后相应地阅读文件

The basic answer is DataInputStream. 基本答案是DataInputStream。 It has methods to read all the primitive datatypes. 它具有读取所有原始数据类型的方法。 When you read which type is of course up to you :-) 当您阅读最适合您的类型时:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM