Java Reference
In-Depth Information
If you want to collect the primes in an array, using the form of get() method that transfers values to an
array is more efficient than writing a loop to transfer them one at a time, but you have to be careful. If the
number of primes in the file is not a multiple of the array size, you must take steps to pick up the last few
stragglers correctly. If you don't, you see the BufferUnderflowException thrown by the get() method.
Let's see it working in an example.
TRY IT OUT: Reading a Binary File
You will read the primes six at a time into an array. Here's the program:
import java.nio.file.*;
import java.nio.*;
import java.nio.channels.ReadableByteChannel;
import java.io.IOException;
public class ReadPrimes {
public static void main(String[] args) {
Path file = Paths.get(System.getProperty("user.home")).
resolve("Beginning Java
Stuff").resolve("primes.bin");
if(!Files.exists(file)) {
System.out.println(file + " does not exist. Terminating
program.");
System.exit(1);
}
final int PRIMECOUNT = 6;
final int LONG_BYTES = 8;
// Number of bytes for
type long
ByteBuffer buf = ByteBuffer.allocate(LONG_BYTES*PRIMECOUNT);
long[] primes = new long[PRIMECOUNT];
try (ReadableByteChannel inCh = Files.newByteChannel(file)){
int primesRead = 0;
while(inCh.read(buf) != -1) {
LongBuffer longBuf =
((ByteBuffer)(buf.flip())).asLongBuffer();
primesRead = longBuf.remaining();
longBuf.get(primes, 0, primesRead);
// List the primes read on the same line
System.out.println();
for(int i = 0 ; i < primesRead ; ++i) {
System.out.printf("%10d", primes[i]);
}
buf.clear();
// Clear the buffer for the
next read
}
System.out.println("\nEOF reached.");
Search WWH ::




Custom Search