java nio copy_使用java NIO实现复制文件

FileChannel src=new FileInputStream(new File(pathName_from)).getChannel();

FileChannel dst=new FileOutputStream(new File(pathName_to)).getChannel();

dst.transferFrom(src, 0, src.size());

可能很多人以前都知道和使用过NIO的很多特性,但是我以前真是没接触过所以受到触动,就学习了更多NIO的东西,然后就实现了下面的这个程序。

服务端:

package com.googlefans.NIO;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.channels.SelectionKey;

import java.nio.channels.Selector;

import java.nio.channels.ServerSocketChannel;

import java.nio.channels.SocketChannel;

import java.util.Iterator;

public class NIOCopyServer implements Runnable{

private FileChannel channel_to;

private Selector selector;

@SuppressWarnings("resource")

public NIOCopyServer(int port) throws IOException{

selector=Selector.open();

ServerSocketChannel serverChannel=ServerSocketChannel.open();

serverChannel.configureBlocking(false);

serverChannel.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(),port));

serverChannel.register(selector, SelectionKey.OP_ACCEPT);

channel_to=new FileOutputStream("E:\\testTo.txt").getChannel();

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

try {

selector.select();

Iterator it=selector.selectedKeys().iterator();

ByteBuffer inBuffer=ByteBuffer.allocate(1024);

if(it.hasNext()){

SelectionKey key=it.next();

it.remove();

if(key.isAcceptable()){

ServerSocketChannel server=(ServerSocketChannel)key.channel();

SocketChannel channel=server.accept();

channel.configureBlocking(false);

while(channel.read(inBuffer)!=-1){

inBuffer.flip();

channel_to.write(inBuffer);

inBuffer.compact();

}

System.out.println("服务器端复制完毕!");

}else if(key.isReadable()){

read(key);

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public void read(SelectionKey key) throws IOException{

SocketChannel channel=(SocketChannel)key.channel();

ByteBuffer byteBuf=ByteBuffer.allocateDirect(1024);

channel.read(byteBuf);

byte[] data=byteBuf.array();

String msg=new String(data).trim();

System.out.println("服务器端收到信息:"+msg);

}

}

客户端:

package com.googlefans.NIO;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.channels.SelectionKey;

import java.nio.channels.Selector;

import java.nio.channels.SocketChannel;

import java.util.Iterator;

public class NIOCopyClient implements Runnable{

private Selector selector;

private FileChannel channel_from;

@SuppressWarnings("resource")

public NIOCopyClient(InetAddress ip,int port) throws IOException{

selector=Selector.open();

SocketChannel channel=SocketChannel.open();

channel.configureBlocking(false);

channel.connect(new InetSocketAddress(ip,port));

channel.register(selector, SelectionKey.OP_CONNECT);

channel_from=new FileInputStream(new File("E:\\testFrom.txt")).getChannel();

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

try {

selector.select();

Iterator it=selector.selectedKeys().iterator();

ByteBuffer outBuffer=ByteBuffer.allocate(1024);

if(it.hasNext()){

SelectionKey key=it.next();

it.remove();

if(key.isConnectable()){

SocketChannel channel=(SocketChannel)key.channel();

if(channel.isConnectionPending()){

channel.finishConnect();

}

channel.configureBlocking(false);

while(channel_from.read(outBuffer)!=-1){

outBuffer.flip();

channel.write(outBuffer);

outBuffer.compact();

}

System.out.println("客户端上传完毕!");

}else if(key.isReadable()){

read(key);

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public void read(SelectionKey key) throws IOException{

SocketChannel channel=(SocketChannel)key.channel();

ByteBuffer bb=ByteBuffer.allocate(1024);

channel.read(bb);

byte[] bytes=bb.array();

String msg=new String(bytes).trim();

System.out.println("客户端收到信息:"+msg);

}

}

实现类:

package com.googlefans.NIO;

import java.io.IOException;

import java.net.InetAddress;

public class NIOCopy {

private final static int PORT=5111;

public static void main(String[] args){

try {

Thread server=new Thread(new NIOCopyServer(PORT));

Thread client=new Thread(new NIOCopyClient(InetAddress.getLocalHost(), PORT));

server.start();

client.start();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

当然我这段程序还是有很多不令人满意的地方,比如read()方法。还是希望各位ITeye的大牛们多多指导。

0

0

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2013-12-30 01:09

浏览 2493

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值