package com.nio;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileChannelTest
{
public static void main(String[] args) throws FileNotFoundException
{
RandomAccessFile randomAccessFile=new RandomAccessFile("/home/plcm/test.txt", "rw");
FileChannel fileChannel=randomAccessFile.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(1024);
try
{
int byteReaded=fileChannel.read(buffer);
while (byteReaded!=-1)
{
System.out.println("Read " + byteReaded);
buffer.flip();
while (buffer.hasRemaining())
{
System.out.println((char)buffer.get());
}
buffer.clear();
byteReaded=fileChannel.read(buffer);
}
fileChannel.close();
}
catch (IOException e)
{
// TODO implement catch IOException
throw new UnsupportedOperationException("Unexpected Exception", e);
}
}
}
Read 25
a
a
a
a
a
a
b
b
b
b
b
c
c
c
c
c
d
d
d
d
d
aaaaaa
bbbbb
ccccc
ddddd
转载请注明原文地址: https://ju.6miu.com/read-1303269.html