Note that there are some explanatory texts on larger screens.

plurals
  1. POReading a binary file in Java
    primarykey
    data
    text
    <p>I have a comparatively long file of unsigned integers (64 bits each, 0.47GB file) that I need to read and store in an array. After some brain racking I wound up using the type long, since everything in Java is signed (correct me if I'm wrong, please) and I couldn't think of a better alternative. Anyhow, the array only has to be sorted, so the precise values of the original numbers are not of the utmost importance. We're supposed to measure the efficiency of the sorting algorithm, nothing more. However, I came up against a brick wall when I actually came to reading the file (my code below).</p> <pre><code>public class ReadFileTest { public static void main(String[] args) throws Exception { String address = "some/directory"; File input_file = new File (address); FileInputStream file_in = new FileInputStream(input_file); DataInputStream data_in = new DataInputStream (file_in ); long [] array_of_ints = new long [1000000]; int index = 0; long start = System.currentTimeMillis(); while(true) { try { long a = data_in.readLong(); index++; System.out.println(a); } catch(EOFException eof) { System.out.println ("End of File"); break; } } System.out.println(index); System.out.println(System.currentTimeMillis() - start); } } </code></pre> <p>It goes on and on forever, and I usually step out to have lunch while the programme's reading. All in all 20 minutes is the fastest I've achieved so far. A course mate bragged today that his programme read it in 4 sec. He's working in C++ and I know that C++ is faster than Java, but this is ridiculous. Could somebody, please, tell me what I'm doing wrong here. I can't blame it on the language or the machine, so it must be me. From what I can see, though, the Java tutorials use exactly the same class, i.e. <code>DataInputStream</code>. I also saw FileChannels being recommended a couple of times. Are they the only way out?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload