通道:表示io源与目标打开的连接(本身不存储数据)
channel类似于传统的流,channel不能直接访问数据,channel只能与buffer进行交互
通道的主要实现类:java.nio.channels.Channel
//本地
FileChannel
//网络
//tcp
SocketChannel
ServerSocketChannel
//udp
DatagramChannel
获取通道:
1. java针对支持通道的类选择了getChannel()方法
本地io
FileInputStream
RandomAccessFile
网络io:
socket
ServerSocket
datagramSocket
2.在jdk1.7nio2针对各个通道提供了静态方法open()
3.在jdk7 中的nio2 files工具类的newByteChannel()
四:通道之间的数据传输
transferForm()
channel类似于传统的流,channel不能直接访问数据,channel只能与buffer进行交互
通道的主要实现类:java.nio.channels.Channel
//本地
FileChannel
//网络
//tcp
SocketChannel
ServerSocketChannel
//udp
DatagramChannel
获取通道:
1. java针对支持通道的类选择了getChannel()方法
本地io
FileInputStream
RandomAccessFile
网络io:
socket
ServerSocket
datagramSocket
2.在jdk1.7nio2针对各个通道提供了静态方法open()
3.在jdk7 中的nio2 files工具类的newByteChannel()
四:通道之间的数据传输
transferForm()
transferTo()
五:分散(scatter)与聚集(gather)
分散读取(Scattering Reads):将通道中的数据分散到多个缓冲区中
聚集写入(Gather Writes):将多个缓冲区中的数据聚集到通道中
六:字符集:charset
编码---》字符数组
解码:字符数组--》:字符串
文件复制:
//传统io流37
public void test() {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
FileChannel channel = null;
FileChannel channel2 = null;
try {
createfile();
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file1);
//获取通道
channel = inputStream.getChannel();
channel2 = outputStream.getChannel();
//分配指定大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
//读取数据,将通道中的数据读入缓冲区
while ((channel.read(buffer))!=-1) {
//将缓冲区中的数据写入通道
buffer.flip();//切换成读取数据模式
channel2.write(buffer);
buffer.clear();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if (outputStream!=null) {
outputStream.close();
}
if (outputStream!=null) {
inputStream.close();
}
if (outputStream!=null) {
channel2.close();
}
if (outputStream!=null) {
channel.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//使用直接缓冲区完成文件的复制(内存映射文件)18
public void test1() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("d:/1.doc"),StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("d:/2.doc"), StandardOpenOption.WRITE,StandardOpenOption.READ);
//内存映射文件
MappedByteBuffer in = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer out = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
//直接对缓冲区进行数组的读写操作
byte[] bs = new byte[in.limit()];
in.get(bs);
out.put(bs);
inChannel.close();
outChannel.close();
System.out.println("文件复制成功");
}
//通道之间的数据传输 transferTo 39
public void test2() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("D:/1.jpg"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("d:/2.jpg"),StandardOpenOption.WRITE,StandardOpenOption.READ);
inChannel.transferTo(0,inChannel.size(),outChannel);
outChannel.close();
inChannel.close();
System.out.println("文件复制成功");
}
//通道之间的数据传输 transferform19
public void test3() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("D:/1.jpg"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("d:/3.jpg"),StandardOpenOption.WRITE,StandardOpenOption.READ);
outChannel.transferFrom(inChannel, 0, inChannel.size());
outChannel.close();
inChannel.close();
System.out.println("文件复制成功");
}
//聚集写入,分散读取39
public void test4() throws IOException {
RandomAccessFile rAccessFile = new RandomAccessFile("d:/1.jpg", "rw");
//1.获取通道
FileChannel channel = rAccessFile.getChannel();
//2.分配指定大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(100);
ByteBuffer buffer2 = ByteBuffer.allocate(1024);
//分散读取
ByteBuffer[] buffers = {buffer,buffer2};
channel.read(buffers);
for (ByteBuffer byteBuffer : buffers) {
byteBuffer.flip();
}
System.out.println(buffers);
System.out.println(new String(buffers[0].array(), 0,buffers[0].limit()));
System.out.println("=================");
System.out.println(new String(buffers[1].array(), 0,buffers[1].limit()));
//聚集写入
RandomAccessFile rAccessFile2 = new RandomAccessFile("2.jpg","rw");
FileChannel channel2 = rAccessFile2.getChannel();
channel2.write(buffers);
}
//charset编解码
public void test5() {
Map<String, Charset> map = Charset.availableCharsets();
Set<Entry<String,Charset>> set = map.entrySet();
for (Entry<String, Charset> entry : set) {
System.out.println(entry.getKey()+"=="+entry.getValue());
}
}
public void test6() throws CharacterCodingException {
Charset charset = Charset.forName("GBK");
//获取编码器
CharsetEncoder en = charset.newEncoder();
//获取解码器
CharsetDecoder de = charset.newDecoder();
CharBuffer charBuffer = CharBuffer.allocate(1024);
charBuffer.put("我是array");
charBuffer.flip();
ByteBuffer buffer = en.encode(charBuffer);
for (int i = 0; i < 8; i++) {
System.out.println(buffer.get());
}
System.out.println("===================");
//解码
buffer.flip();
CharBuffer cb = de.decode(buffer);
System.out.println(cb.toString());
}