Cómo leer en writeBin (salida) de R en Java?

clarinetista:

Considere el siguiente código R:

f <- file("test.txt", "wb")
writeBin(c(1.2, 2.3, 3.4), f)
close(f)

He escrito el siguiente código en Java para tratar de leer estos datos:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.PrintWriter;

public class readBinary {

    public static void main(String[] argv) throws IOException {
        DataInputStream fileIn = new DataInputStream(new FileInputStream(argv[0]));
        FileOutputStream fileOut = new FileOutputStream(argv[1]);
        PrintWriter fileOutWriter = new PrintWriter(fileOut);

        int rows = Integer.parseInt(argv[2]);
        int cols = Integer.parseInt(argv[3]);

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                fileOutWriter.print(fileIn.readDouble() + " ");
            }
            fileOutWriter.println();
        }
        fileOutWriter.close();
    }

}

En la línea de comandos:

javac readBinary.java
java readBinary test.txt test_new.txt 3 1

Las salidas de archivos test_new.txt:

4.667261458438315E-62 
1.9035985662475526E185 
4.667261458387024E-62 

Así que Java no es recoger esto como tres dobles correctamente.

Por lo tanto, pregunto, ¿qué es exactamente la salida de writeBin, y como no podía ser leído en Java, por lo que los números destinados se leen correctamente?

Andrey Shabalin:

Java en bigEndian por defecto. Por lo tanto por favor, trate de usarwriteBin(..., endian = "big")

f <- file("test.txt", "wb")
writeBin(c(1.2, 2.3, 3.4), f, endian = "big")
close(f)

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=397225&siteId=1
Recomendado
Clasificación