java nio使用样例_Java NIO系列使用示例

1 packagecom.nio.test;2

3 importjava.io.IOException;4 importjava.io.RandomAccessFile;5 importjava.net.InetSocketAddress;6 importjava.nio.ByteBuffer;7 importjava.nio.CharBuffer;8 importjava.nio.channels.DatagramChannel;9 importjava.nio.channels.FileChannel;10 importjava.nio.channels.Pipe;11 importjava.nio.channels.ServerSocketChannel;12 importjava.nio.channels.SocketChannel;13 importjava.nio.charset.Charset;14 importjava.nio.charset.CharsetDecoder;15 importjava.nio.charset.CoderResult;16 importjava.nio.file.Files;17 importjava.nio.file.LinkOption;18 importjava.nio.file.Path;19 importjava.nio.file.Paths;20

21 public classChannelTest {22 public static void main(String[] args) throwsException {23 newChannelTest().filewrite();24 newChannelTest().byteBufferUtf8();25 newChannelTest().fileread();26 newChannelTest().clientsocket();27 newChannelTest().serverSocket();28 newChannelTest().serverDatagram();29 newChannelTest().clientDatagram();30 newChannelTest().pipe();31 newChannelTest().NIOPath();32 }33

34 private voidfileread() {35 RandomAccessFile aFile;36 Charset charset = Charset.forName("UTF-8");37 CharsetDecoder decoder =charset.newDecoder();38 try{39 //在使用FileChannel之前,必须先打开它。但是,我们无法直接打开一个FileChannel,40 //需要通过使用一个InputStream、OutputStream或RandomAccessFile来获取一个FileChannel实例

41 aFile = new RandomAccessFile("src/com/nio/test/nio-data.txt", "rw");42

43 FileChannel inChannel =aFile.getChannel();44 //首先,分配一个Buffer。从FileChannel中读取的数据将被读到Buffer中。45 //create buffer with capacity of 48 byte

46 ByteBuffer byteBuffer = ByteBuffer.allocate(48);//read into buffer.

47 CharBuffer charBuffer = CharBuffer.allocate(48);48

49 //调用多个read()方法之一 从FileChannel中读取数据。

50 int bytesRead =inChannel.read(byteBuffer);51

52 char[] tmp = null; //临时存放转码后的字符

53 byte[] remainByte = null;//存放decode操作后未处理完的字节。decode仅仅转码尽可能多的字节,此次转码不了的字节需要缓存,下次再转

54 int leftNum = 0; //未转码的字节数

55

56 while (bytesRead != -1) {57

58 //System.out.println("Read " + bytesRead);

59 byteBuffer.flip(); //make buffer ready for read

60 decoder.decode(byteBuffer, charBuffer, false);61

62 charBuffer.flip();63

64 remainByte = null;65 leftNum = byteBuffer.limit() -byteBuffer.position();66 if (leftNum > 0) { //记录未转换完的字节

67 remainByte = new byte[leftNum];68 byteBuffer.get(remainByte, 0, leftNum);69 }70

71 //输出已转换的字符

72 tmp = new char[charBuffer.length()];73 while(charBuffer.hasRemaining()) {74 charBuffer.get(tmp);75 System.out.print(newString(tmp));76 }77

78 byteBuffer.clear(); //make buffer ready for writing

79 charBuffer.clear();80

81 if (remainByte != null) {82 byteBuffer.put(remainByte); //将未转换完的字节写入bBuf,与下次读取的byte一起转换

83 }84

85 bytesRead =inChannel.read(byteBuffer);86 }87

88 aFile.close();89 } catch(Exception e) {90 //TODO Auto-generated catch block

91 e.printStackTrace();92 }93 }94

95 private void filewrite() throwsException {96 RandomAccessFile accessFile = new RandomAccessFile("src/com/nio/test/nio-data11.txt", "rw");97 FileChannel fileChannel =accessFile.getChannel();98 String newDate = "New String to write to file" +System.currentTimeMillis();99 ByteBuffer buffer = ByteBuffer.alloc

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值