Java——NIO

1.缓冲区Buffer


import java.nio.IntBuffer;
//主类
//Function : IntBuffer_demo
public class IntBuffer_demo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
IntBuffer buf = IntBuffer.allocate(10); //开辟10个大小的缓冲区
System.out.print("1.写入数据之前的position、limit和capacity");
System.out.println("position="+buf.position()+"、limit="+buf.limit()+"、capacity="+buf.capacity());
int temp[] = {3,5,7}; //定义整型数组
buf.put(3); //向缓冲区写入数据
buf.put(temp); //向缓冲区中写入一组数据
System.out.print("2.写入数据之后的position、limit和capacity");
System.out.println("position="+buf.position()+"、limit="+buf.limit()+"、capacity="+buf.capacity());
buf.flip(); //重设缓冲区,改变指针
System.out.print("3.准备输出数据时的position、limit和capacity");
System.out.println("position="+buf.position()+"、limit="+buf.limit()+"、capacity="+buf.capacity());
while(buf.hasRemaining()){
int x = buf.get();
System.out.print(x+"、");
}
}
}



创建子缓冲区
import java.nio.IntBuffer;
//主类
//Function : IntBuffer_demo
public class IntBuffer_demo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
IntBuffer buf = IntBuffer.allocate(10); //开辟10个大小的缓冲区
IntBuffer sub = null; //定义缓冲区对象
for(int i=0;i<10;i++){
buf.put(2*i+1);
}
buf.position(2);
buf.limit(6);
sub = buf.slice(); //开辟子缓冲区
for(int i=0;i<sub.capacity();i++){
int temp = sub.get(i);
sub.put(temp-1);
}
buf.flip(); //重设缓冲区
buf.limit(buf.capacity()); //设置limit
System.out.println("主缓冲区中的内容:");
while(buf.hasRemaining()){
int x = buf.get(); //取出当前内容
System.out.print(x+"、");
}
}
}


import java.nio.IntBuffer;
//主类
//Function : IntBuffer_demo
public class IntBuffer_demo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
IntBuffer buf = IntBuffer.allocate(10); //开辟10个大小的缓冲区
IntBuffer read = null; //定义缓冲区对象
for(int i=0;i<10;i++){
buf.put(2*i+1);
}
read = buf.asReadOnlyBuffer(); //创建只读缓冲区
buf.flip(); //重设缓冲区
System.out.println("主缓冲区中的内容:");
while(buf.hasRemaining()){
int x = buf.get(); //取出当前内容
System.out.print(x+"、");
}
System.out.println();
read.put(30); //错误,不可写
}
}


2.通道Channel

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//主类
//Function : FileChannel_demo
public class FileChannel_demo {
public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
String info[] = {"123","456","789"};
File f = new File("/home/common/software/coding/HelloWord/HelloWord/out.txt");//路径
FileOutputStream output = null;
output = new FileOutputStream(f);
FileChannel fout = null; //声明输出的通道
fout = output.getChannel(); //得到输出的文件通道
ByteBuffer buf = ByteBuffer.allocate(1024); //开辟缓冲
for(int i=0;i<info.length;i++){
buf.put(info[i].getBytes());
}
buf.flip(); //重设缓冲区,准备输出
fout.write(buf); //输出
fout.close();
output.close();
}
}
读写文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//主类
//Function : FileChannel_demo
public class FileChannel_demo {
public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
File f1 = new File("/home/common/software/coding/HelloWord/HelloWord/out.txt");//路径
File f2 = new File("/home/common/software/coding/HelloWord/HelloWord/outnote.txt");//路径
FileInputStream input = null;
FileOutputStream output = null;
input = new FileInputStream(f1);
output = new FileOutputStream(f2);
FileChannel fin = null; //声明输入的通道
FileChannel fout = null; //声明输出的通道
fin = input.getChannel(); //得到输入的文件通道
fout = output.getChannel(); //得到输出的文件通道
ByteBuffer buf = ByteBuffer.allocate(1024); //开辟缓冲
int temp = 0; //声明变量接收内容
while((temp=fin.read(buf)) != -1){
buf.flip();
fout.write(buf);
buf.clear();
}
fin.close();
fout.close();
input.close();
output.close();
}
}


3.Selector




本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/5324181.html

浙公网安备 33010602011771号