nio与io

作者: 靈08_1024 | 来源:发表于2017-06-17 16:25 被阅读15次

io包是以字节流(stream)、字符流(reader/writer)来进行文件的同步读写。
nio是以通道(channel)的方式进行一步读取。是JDK4以后提出,从写法或者类库上都比io简洁,但必须依赖于io来创建。
旧io的文件复制:

BufferedReader br = new BufferedReader(new FileReader(new File(path)));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path + "z")));
        String s;
        while ((s = br.readLine())!=null){
            pw.write(s+"\n");
        }
        br.close();
        pw.close();

nio的文件复制:

FileChannel in = new FileInputStream("../fc.in").getChannel();
            FileChannel out = new FileOutputStream("../fc.out1").getChannel();
//type 1:
//            ByteBuffer buffer = ByteBuffer.allocate(GetChannel.BSIZE);
//            while (in.read(buffer)!=-1){
//                buffer.flip();
//                out.write(buffer);
//                buffer.clear();
//            }

            //type 2:
            in.transferTo(0,in.size(),out);
            //type 3:
            out.transferFrom(in,0,in.size());

nio在读入文件时,需要依靠ByteBuffer来存储读到的字节数组,ByteBuffer.flip()可以开启快速存取模式(将当前位置置0),每次读取完下一次读取前需要清除缓存ByteBuffer.clear()。
-- 在ByteBuffer中,asCharBuffer()方法会将字节以字符的形式输出。该方法存在弊端:

public class BufferToText {
    public static int BSIZE=1024;
    static String path="../data2.out";
    public static void main(String[] args) throws IOException {
        FileChannel fc = new FileOutputStream(path).getChannel();
        fc.write(ByteBuffer.wrap("wa haha haha    \n".getBytes()));//write sth
        fc.close();
        //Read the file
        fc = new FileInputStream(path).getChannel();
        ByteBuffer buff = ByteBuffer.allocate(BSIZE);
        fc.read(buff);
        buff.flip();
        System.out.println(buff.asCharBuffer());//此处采用JAVA默认的UTF-16BE来转换,会乱码
        buff.rewind();
        String encoding = System.getProperty("file.encoding");//the current java file's encoding.
        //此处不会乱码,因为采用系统默认的编码编写,此处采用系统默认编码解码
        System.out.println("Decoded using"+encoding+":"+ Charset.forName(encoding).decode(buff));
//-------------------------------------------------
        fc = new FileOutputStream(path).getChannel();
        fc.write(ByteBuffer.wrap(("hanhong hui huahua ").getBytes("UTF-16be")));
        fc.close();

        //re-read
        fc = new FileInputStream(path).getChannel();
        buff.clear();
        fc.read(buff);
        buff.flip();

        System.out.println(buff.asCharBuffer());
        //------------------------------------------------

        fc = new FileOutputStream(path).getChannel();
        buff = ByteBuffer.allocate(100);
        buff.asCharBuffer().put("ai xiong xiong    ");
        fc.write(buff);
        fc.close();

        //read and display
        fc = new FileInputStream(path).getChannel();
        buff.clear();
        fc.read(buff);
        buff.flip();
        System.out.println(buff.asCharBuffer());
    }
}

相关文章

  • note

    Java IO,NIO,NIO2 以及与操作系统,磁盘 IO NIO模型selector NIO的核心是IO线程池...

  • Java NIO

    1、IO和NIO的区别? 1)IO面向流、NIO面向缓冲;2)IO是阻塞IO、NIO是非阻塞IO;3)无 与 选择...

  • java NIO详解

    NIO原理 NIO与IO的区别 首先来讲一下传统的IO和NIO的区别,传统的IO又称BIO,即阻塞式IO,NIO就...

  • 29、 Java IO与 NIO的区别(补充)

    Java IO与 NIO的区别(补充) NIO即New IO,这个库是在JDK1.4中才引入的。NIO和IO有相同...

  • Java NIO

    # Java NIO # Java NIO属于非阻塞IO,这是与传统IO最本质的区别。传统IO包括socket和文...

  • 图解Java NIO

    目录: NIO结构 NIO与传统IO异同 NIO使用步骤 NIO代码 ByteBuffer难点解析 1:NIO结构...

  • Java常见面试题汇总-----------Java基础(NIO

    18. NIO与IO的区别   NIO即New IO,这个库是在JDK1.4中才引入的。NIO和IO有相同的作用和...

  • 18道IO常问面试题,题题惊险!

    大厂招聘IO常问面试题 NIO与IO的区别 NIO和IO适用场景 BIO, NIO, AIO有什么区别,分别是什么...

  • Java之NIO(非阻塞IO)

    【1】NIO的与IO的区别: 总的来说java 中的IO 和NIO的区别主要有3点:1)IO是面向流的,NIO是面...

  • java-nio学习之java io比较

    java io 基本介绍 java nio和io区别 面向流与面向缓冲 IO是面向流的,NIO是面向缓冲区的。 J...

网友评论

      本文标题:nio与io

      本文链接:https://www.haomeiwen.com/subject/jnivqxtx.html