Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何读取二进制文件中的整数和日期_Java_Binary - Fatal编程技术网

Java 如何读取二进制文件中的整数和日期

Java 如何读取二进制文件中的整数和日期,java,binary,Java,Binary,我有一个二进制记录文件。每条记录的格式如下: 每个记录由两个整数组成,即涉及的车辆数量和人员数量;浮点数,严重性代码;和一个包含事故日期的字符串。该日期采用以下形式:三个字母的月份缩写,后跟空格,然后是月份的一天,后跟逗号,最后是年份,年份可以用两位数或四位数表示 下面是代码。但是我没有得到正确的答案 我的代码: import java.io.*; public class BoydBAssignment5_Ver1 { public static void main(String

我有一个二进制记录文件。每条记录的格式如下: 每个记录由两个整数组成,即涉及的车辆数量和人员数量;浮点数,严重性代码;和一个包含事故日期的字符串。该日期采用以下形式:三个字母的月份缩写,后跟空格,然后是月份的一天,后跟逗号,最后是年份,年份可以用两位数或四位数表示

下面是代码。但是我没有得到正确的答案

我的代码:

import java.io.*;

public class BoydBAssignment5_Ver1 {

    public static void main(String s[]) {
        DataInputStream input1;                     //you need these two variable for a file
        File            infile1;
        input1 = null;

        BoydBAssignment5_Ver1 tfr;                  //this is your program object
        tfr = new BoydBAssignment5_Ver1();
        try{    //try for open
            infile1 = new File("assign5.data");
            input1 = new DataInputStream(new FileInputStream(infile1));
        } catch (IOException i){
            i.printStackTrace();}

        tfr.read_records(input1);
        try {   //try for close
            input1.close();
        } catch (IOException i) {
            System.out.println("error in close");
        }
    }

    private void read_records(DataInputStream is2) {
        int totalVehicles=0,totalPersons=0;
        int numVehicles;
        int numPeople;
        char ch;

        try {   //try for read
            while(true) {
                numVehicles=is2.readInt();
                if(numVehicles==0)
                break;

                totalVehicles+=numVehicles;
                System.out.print("\n"+numVehicles+"\t");
                numPeople=is2.readInt();
                if(numPeople==0)
                break;

                totalPersons+=numPeople;
                System.out.print(numPeople+"\t");
                System.out.print(is2.readDouble()+"\t");
                /*System.out.print(is2.readLine()+"\n");
                    for(int k=0;k<4;k++)
                    {
                        is2.readByte();
                    }*/
                while((ch=(char)is2.readByte()) != 0x00) {
                    System.out.print(ch+"");
                }
            }

            System.out.println("\nTotal no of vehicles:"+totalVehicles);
            System.out.println("Total no of Persons:"+totalPersons);
            write_in_file(totalVehicles,totalPersons);
        } catch (IOException i) {
            System.out.println("error in write");
        }

    }

    private void write_in_file(int totalVehicles, int totalPersons) {
        try {
            FileWriter fstream = new FileWriter("finalOutput.data");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("Number of vehicles involved"+"\t"+totalVehicles);
            out.write("\nNumber of persons involved"+"\t"+totalPersons);
            out.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
import java.io.*;
公共类BoydAssignment5_Ver1{
公共静态void main(字符串s[]{
DataInputStream input1;//文件需要这两个变量
文件填充1;
input1=null;
BoydBasignment5_Ver1 tfr;//这是您的程序对象
tfr=新的BoydAssignment5_Ver1();
try{//try for open
infile1=新文件(“assign5.data”);
input1=新数据输入流(新文件输入流(infie1));
}捕获(IOI异常){
i、 printStackTrace();}
tfr.读取记录(输入1);
try{//try for close
input1.close();
}捕获(IOI异常){
System.out.println(“关闭时出错”);
}
}
私有无效读取记录(DataInputStream is2){
int totalVehicles=0,totalPersons=0;
国际货币基金组织;
人民内部;
char ch;
try{//try for read
while(true){
numVehicles=is2.readInt();
如果(numVehicles==0)
打破
车辆总数+=车辆数量;
系统输出打印(“\n”+numVehicles+“\t”);
numpople=is2.readInt();
如果(numpople==0)
打破
总人数+=人数;
系统输出打印(numpople+“\t”);
System.out.print(is2.readDouble()+“\t”);
/*System.out.print(is2.readLine()+“\n”);

对于(int k=0;k的猜测是,您的数据存储为小尾端。
DataInputStream
是大尾端。(如果您不确定尾端是什么,请参见)


最简单的方法是将文件读入一个直接的
ByteBuffer
,它允许您设置字节顺序。

您确定整数是32位的吗?此外,
readDouble
方法将读取64字节的浮点信息。在您的输出中有日期,但在代码中没有读取任何日期。是吗你确定这段代码生成了那个输出吗?是的,这个给定的输出与代码相对应,我在((ch=(char)是2.readByte())!=0x00){System.out.print(ch+“”);}对于以字符串形式读取的日期,请告诉我,如果有其他方法,请提前告诉我..谢谢..鉴于第二个整数是“1”,这里的情况似乎不是这样的;我怀疑,由于日期没有出现在某些行上,整数的字节大小可能会变小。谢谢你,这一点…你能告诉我如何转换b吗ig endian到little endian,反之亦然..提前感谢!!使用
ByteBuffer.order(ByteOrder.little\u endian)
ByteBuffer.order(ByteOrder.BIG\u endian)