zoukankan      html  css  js  c++  java
  • Java NIO

    读取指定文件的类容:

    package com.nio;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class FileInputStreamTest {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File file = new File("d:\reader.txt");
            FileInputStream in = new FileInputStream(file);
            FileChannel channel = in.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
            
            channel.read(buffer);
            buffer.rewind();
            byte[] b = buffer.array();
            System.out.println(new String(b));
            
            channel.close();
            in.close();
        }
    
    }

    文件拷贝:

    package com.nio;
    
    import java.io.FileInputStream;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class CopyFileTest {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            FileInputStream fin = new FileInputStream("d:\reader.txt");
            FileOutputStream fout = new FileOutputStream("d:\nioTest.txt");
            FileChannel inChannel = fin.getChannel();
            FileChannel outChannel = fout.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(128);
            while(true){
                buffer.clear();
                int r = inChannel.read(buffer);
                if(r==-1){
                    break;
                }
                buffer.flip();
                outChannel.write(buffer);
            }
            outChannel.close();
            inChannel.close();
            fout.close();
            fin.close();
        }
    
    }

     参考文档:http://ifeve.com/buffers/

    socket:http://blog.csdn.net/kongxx/article/details/7288896

    http://developer.51cto.com/art/201112/306363.htm

  • 相关阅读:
    RabbitMQ 路由选择 (Routing)
    RabbitMQ 发布/订阅
    RabbitMQ 工作队列
    MySQL中的insert ignore into, replace into等的一些用法总结
    BigDecimal用法详解
    RabbitMQ 入门 Helloworld
    git标签
    git查看提交历史
    RabbitMQ简介
    【计算机视觉】SeetaFace Engine开源C++人脸识别引擎
  • 原文地址:https://www.cnblogs.com/james-roger/p/5683948.html
Copyright © 2011-2022 走看看