Java Reference
In-Depth Information
TRY IT OUT: Reading Mixed Data from a File
Here's the complete program code:
import java.nio.file.*;
import java.nio.channels.FileChannel;
import java.io.IOException;
import java.nio.ByteBuffer;
public class ReadPrimesMixedData {
public static void main(String[] args) {
Path file = Paths.get(System.getProperty("user.home")).
resolve("Beginning Java
Stuff").resolve("primes.txt");
if(!Files.exists(file)) {
System.out.println(file + " does not exist. Terminating
program.");
System.exit(1);;
}
try (FileChannel inCh = (FileChannel)Files.newByteChannel(file)){
ByteBuffer lengthBuf = ByteBuffer.allocate(8);
int strLength = 0;
// Stores the
string length
ByteBuffer[] buffers = {
null,
// Byte buffer to
hold string
ByteBuffer.allocate(8) // Byte buffer to
hold prime
};
while(true) {
if(inCh.read(lengthBuf) == -1)
// Read the string
length,
break;
// if its EOF exit
the loop
lengthBuf.flip();
// Extract the length and convert to int
strLength = (int)lengthBuf.getDouble();
// Now create the buffer for the string
buffers[0] = ByteBuffer.allocate(2*strLength);
if(inCh.read(buffers) == -1) { // Read the string & binary
prime value
Search WWH ::




Custom Search