public class NioTest
{
public static void main(String[] args) throws IOException
{
copyFile();
}
//拷贝文件
private static void copyFile()
{
FileInputStream in=null;
FileOutputStream out=null;
try
{
in=new FileInputStream("src/main/java/data/in-data.txt");
out=new FileOutputStream("src/main/java/data/out-data.txt");
FileChannel inChannel=in.getChannel();
FileChannel outChannel=out.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(1024);
int bytesRead = inChannel.read(buffer);
while (bytesRead!=-1)
{
buffer.flip();
outChannel.write(buffer);
buffer.clear();
bytesRead = inChannel.read(buffer);
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//写文件
private static void writeFileNio()
{
try
{
RandomAccessFile fout = new RandomAccessFile("src/main/java/data/nio-data.txt", "rw");
FileChannel fc=fout.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(1024);
buffer.put("hi123".getBytes());
buffer.flip();
try
{
fc.write(buffer);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//读文件
private static void readFileNio()
{
FileInputStream fileInputStream;
try
{
fileInputStream = new FileInputStream("src/main/java/data/nio-data.txt");
FileChannel fileChannel=fileInputStream.getChannel();//从 FileInputStream 获取通道
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);//创建缓冲区
int bytesRead=fileChannel.read(byteBuffer);//将数据读到缓冲区
while(bytesRead!=-1)
{
/*limit=position
* position=0;
*/
byteBuffer.flip();
//hasRemaining():告知在当前位置和限制之间是否有元素
while (byteBuffer.hasRemaining())
{
System.out.print((char) byteBuffer.get());
}
/*
* 清空缓冲区
* position=0;
* limit=capacity;
*/
byteBuffer.clear();
bytesRead = fileChannel.read(byteBuffer);
}
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
selector=Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while(true)
{
try
{
//select()阻塞到至少有一个通道在你注册的事件上就绪了
//如果没有准备好的channel,就在这一直阻塞
//select(long timeout)和select()一样,除了最长会阻塞timeout毫秒(参数)。
selector.select();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
}
//返回已经就绪的SelectionKey,然后迭代执行
Set<SelectionKey> readKeys=selector.selectedKeys();
for(Iterator<SelectionKey> it=readKeys.iterator();it.hasNext();)
{
SelectionKey key=it.next();
it.remove();
try
{
if(key.isAcceptable())
{
ServerSocketChannel server=(ServerSocketChannel) key.channel();
SocketChannel client=server.accept();
client.configureBlocking(false);
client.register(selector,SelectionKey.OP_WRITE);
}
else if(key.isWritable())
{
SocketChannel client=(SocketChannel) key.channel();
ByteBuffer buffer=ByteBuffer.allocate(20);
String str="hello";
buffer=ByteBuffer.wrap(str.getBytes());
client.write(buffer);
key.cancel();
}
}catch(IOException e)
{
e.printStackTrace();
key.cancel();
try
{
key.channel().close();
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if(key.isAcceptable())
{
ServerSocketChannel server=(ServerSocketChannel) key.channel();
SocketChannel client=server.accept();
client.configureBlocking(false);
...
}
client.register(selector,SelectionKey.OP_WRITE);
else if(key.isWritable())
{
SocketChannel client=(SocketChannel) key.channel();
ByteBuffer buffer=ByteBuffer.allocate(20);
String str="hello";
buffer=ByteBuffer.wrap(str.getBytes());
client.write(buffer);
key.cancel();
}
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class ServerSocketChannelDemo
{
public static void main(String[] args)
{
ServerSocketChannel serverSocketChannel;
Selector selector=null;
try
{
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
selector=Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true)
{
try
{
//select()阻塞到至少有一个通道在你注册的事件上就绪了
//如果没有准备好的channel,就在这一直阻塞
//select(long timeout)和select()一样,除了最长会阻塞timeout毫秒(参数)。
selector.select();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
//返回已经就绪的SelectionKey,然后迭代执行
Set<SelectionKey> readKeys=selector.selectedKeys();
for (Iterator<SelectionKey> it=readKeys.iterator();it.hasNext();)
{
SelectionKey key=it.next();
it.remove();
try
{
if(key.isAcceptable())
{
ServerSocketChannel server=(ServerSocketChannel) key.channel();
SocketChannel client=server.accept();
client.configureBlocking(false);
client.register(selector,SelectionKey.OP_WRITE);
} else if(key.isWritable())
{
SocketChannel client=(SocketChannel) key.channel();
ByteBuffer buffer=ByteBuffer.allocate(20);
String str="hello";
buffer=ByteBuffer.wrap(str.getBytes());
client.write(buffer);
key.cancel();
}
}
catch(IOException e)
{
e.printStackTrace();
key.cancel();
try
{
key.channel().close();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有