62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws IOException{
FileChannel channel = new FileOutputStream("D:/a.txt").getChannel();
// 字节方式写入
channel.write(ByteBuffer.wrap("hello, NIO world in java!".getBytes()));
channel.close();
// 根据FileInputStream获得通道FileChannel
channel = new FileInputStream("D:/a.txt").getChannel();
// ByteBuffer分配空间,16个字节
// 这里需要知道 byte是1字节, short和char是2字节,int和float是4字节
// long和double是8字节 1byte=8bit 。 基本只是还是必须记住的。
ByteBuffer buff = ByteBuffer.allocate(16);
// 字节数组数据装入buff,
channel.read(buff);
// 反转此缓冲区
buff.flip();
byte [] byt = new byte[10];
System.out.println(buff.get(byt));
channel.close();
}
ByteBuffer buf = ByteBuffer.allocate(50); //这里要改大
chf.read(buf); //这句话抛的异常
buf.flip();
byte [] byt = new byte[100];
buf.get(byt);
public static void main(String[] args) throws IOException{
FileChannel channel = new FileOutputStream("D:/a.txt").getChannel();
// 字节方式写入
channel.write(ByteBuffer.wrap("hello, NIO world in java!".getBytes()));
channel.close();
// 根据FileInputStream获得通道FileChannel
channel = new FileInputStream("D:/a.txt").getChannel();
// ByteBuffer分配空间,16个字节
// 这里需要知道 byte是1字节, short和char是2字节,int和float是4字节
// long和double是8字节 1byte=8bit 。 基本只是还是必须记住的。
ByteBuffer buff = ByteBuffer.allocate(16);
// 字节数组数据装入buff,
channel.read(buff);
// 反转此缓冲区
buff.flip();
byte [] byt = new byte[10];
System.out.println(buff.get(byt)); // 根据FileOutputStream获得通道FileChannel
}