Rollerball
Rollerball

Reputation: 13108

Oracle snippet does not give back results

Here I have the following bit of code taken from this oracle java tutorial:

// Defaults to READ
try (SeekableByteChannel sbc = Files.newByteChannel(file)) {
    ByteBuffer buf = ByteBuffer.allocate(10);

    // Read the bytes with the proper encoding for this platform.  If
    // you skip this step, you might see something that looks like
    // Chinese characters when you expect Latin-style characters.
    String encoding = System.getProperty("file.encoding");
    while (sbc.read(buf) > 0) {
        buf.rewind();
        System.out.print(Charset.forName(encoding).decode(buf));
        buf.flip();//LINE X
    }
} catch (IOException x) {
    System.out.println("caught exception: " + x);

So basically I do not get any output out of it. I have tried to put some flags in the while loop to check whether or not it gets into, and it gets into. I also changed the encoding in Charset.defaultCharset().decode(buf), result : no output. Of course there is text in the file passed to newByteChannel(file);

Any idea? Thanks a lot in advance.

**

EDIT:

** Solved, it was just the file I was trying to access that had been previously accidentally corrupted. After having changed file, everything is working.

Upvotes: 0

Views: 58

Answers (1)

user207421
user207421

Reputation: 310980

The code looks wrong. Try changing the rewind() to flip(), and the flip() to compact().

Upvotes: 1

Related Questions