简体   繁体   English

从文件中读取二进制数,并从longs / doubles中读取整数

[英]Read binary numbers from file and separate integers from longs/doubles

In this programmer i found Prime numbers in first 100.Numbers are in INT format and totally number of them is in DOUBLE format.I want to read that file and i did it for only INT numbers but i dont know hot to do it for DOUBLE number. 在这个程序员中,我发现前100个质数。数字是INT格式的,它们的总数都是DOUBLE格式的。我想读取该文件,并且只对INT编号进行处理,但我不知道为DOUBLE做它数。 Here is the code: 这是代码:

package int1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.channels.FileChannel;

public class int_upis {
public static void main(String[] args) {
    File a = new File("C:\\Users\\Jovan\\Desktop\\Pisem.txt");
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(a);
    } catch (Exception e) {

    }
    FileChannel ch = fos.getChannel();
    ByteBuffer bff = ByteBuffer.allocate(100);
    IntBuffer ibf = bff.asIntBuffer(); // Int type
    DoubleBuffer db = bff.asDoubleBuffer(); // Double type
    double p = 0;
    for (int i = 1; i <= 100; i++) {
        int t = 0;
        for (int j = 1; j <= i; j++) {
            if (i % j == 0) {
                t = t + 1;
            }

        }
        if (t < 3) {
            p = p + 1; // number of Prime numbers
            System.out.println(i);
            ibf.put(i);
            bff.position(4 * ibf.position());
            bff.flip();
            try {
                ch.write(bff);
                bff.clear();
                ibf.clear();
            } catch (IOException e) {

            }
        }

    }

    try {
        db.put(p); //At the end of the txt-file i put double format of number (Number of Prime numbers)
        bff.position(8*db.position());
        bff.flip();
        ch.write(bff);

        System.out.println("File is writen with: " + ch.size());
        ch.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

Now I tried to read this file: 现在,我尝试读取此文件:

public class int_ispis {
public static void main(String[] args) throws IOException {
    File a = new File("C:\\Users\\Jovan\\Desktop\\Pisem.txt");
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(a);
    } catch (Exception e) {

    }
    FileChannel ch = fis.getChannel();
    ByteBuffer bff = ByteBuffer.allocate(6 * 4);

This is one line of Prime Number put in a 6-row array (this line below): 这是放在6行数组中的素数行(下面的行):

    int[] niz = new int[6];
    System.out.println("Pre flipa: " + bff.position() + " " + bff.limit());
    System.out.println("++++++++++++++++++++++++++++++++++++");
    while (ch.read(bff) != -1) {

        bff.flip();
        System.out.println();
        System.out.println("Posle flipa: " + bff.position() + " " + bff.limit());
        IntBuffer ib = bff.asIntBuffer();
        System.out.println("IB: " + ib.position() + " " + ib.limit());

        int read = ib.remaining();
        System.out.println(read);

When it come to the end of file it puts Double Number as Integer and writes wrong number(How to separate Integer form Double number?) 当到达文件末尾时,将Double Number作为Integer并写入错误的数字(如何将Integer格式与Double Number分开?)

        ib.get(niz, 0, ib.remaining());
        for (int i = 0; i < read; i++) {
            System.out.print(niz[i] + " ");

        }
        System.out.println();
        System.out.println("=================================");
        ib.clear();
        bff.clear();

    }
}
}

A binary file does not have any "separators". 二进制文件没有任何“分隔符”。

You need to know the structure of the file content and use this knowledge. 您需要了解文件内容的结构并使用此知识。

In this programmer i found Prime numbers in first 100.Numbers are in INT format and totally number of them is in DOUBLE format. 在这个程序员中,我发现素数是前100个。数字是INT格式的,它们的总数是DOUBLE格式的。

This means that there is only one long value in the file and this is in the last 8 bytes . 这意味着文件中只有一个long值,并且在最后8个字节中 So you simply have to check if the current position is fileLenght - 8 and then read these last 8 bytes as a long value. 因此,您只需要检查当前位置是否为fileLenght - 8 ,然后将这最后8个字节读取为long值。

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

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