Java 中的IO

作者: 奔跑的笨鸟 | 来源:发表于2017-06-21 21:13 被阅读11次

Java Old IO

Java IO是通过Stream 以block方式读取数据的。其中包括字节和字符Stream.
下面一个小例子,分别用字节和字符流来读取文件。

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class OldIOTest {
    static String fileName="c:/test.txt";
    private void readFileByByte(String fileName) {
        BufferedInputStream bufferedInputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(fileName)));
            byte[] data = new byte[1024];
            int len;
            while ((len = bufferedInputStream.read(data)) > 0) {
                System.out.println(new String(data, 0, len));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void  readFileByChar(String fileName) {
        BufferedReader bReader= null;
        try {
             bReader=new BufferedReader(new FileReader(new File(fileName)));
             String line;
             while((line=bReader.readLine())!=null){
                 System.out.println(line);
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bReader!=null){
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        OldIOTest oldIOTest = new OldIOTest();
        oldIOTest.readFileByByte(fileName);
        System.out.println("------------------------------------------");
        oldIOTest.readFileByChar(fileName);
    }

}

执行结果;

test java io
this is a test file.
------------------------------------------
test java io
this is a test file.

Java NIO

Java NIO 是通过Buffer以非block方式读取数据的。NIO 将最耗时的 I/O 操作转移回操作系统,因而可以极大地提高速度。
其中重要的几个概念:

  • Channels
  • Buffers
  • Selectors
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOTest {
    static String fileName = "c:/test.txt";

    private void readFile(String fileName) {
        try {
            @SuppressWarnings("resource")
            FileChannel channel = new FileInputStream(new File(fileName)).getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(64);
            while (channel.read(buffer) != -1) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    System.out.print((char) buffer.get());

                }
                buffer.clear();
            }

            channel.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void write(String fileName) {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(new File(fileName), "rw");
            FileChannel channel = randomAccessFile.getChannel();
            channel.position(randomAccessFile.length());
            ByteBuffer buffer = ByteBuffer.allocate(64);
            buffer.put("\nnew test.".getBytes());
            buffer.flip();
            channel.write(buffer);
            channel.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        NIOTest nioTest = new NIOTest();
        nioTest.write(fileName);
        nioTest.readFile(fileName);

    }
}

执行结果:

test java io
this is a test file.
new test.

参考:
http://tutorials.jenkov.com/java-nio/index.html
https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html

相关文章

  • Java中的IO模型

    Java中的IO模型 Java中的IO模型有四种: 同步阻塞IO 同步非阻塞IO IO多路复用 异步IO 其中IO...

  • IO

    一 IO中各个流的基本用法 参考: java中的IO整理 二 IO体系 参考:看完这个,Java IO从此不在难 ...

  • 10. Java IO: Readers and Writers

    想要查看此教程的目录请点击:Java IO教程目录贴地址 Java IO中的java.io.Reader和java...

  • 详解IO复用模型select,poll,epoll机制

    在Java中,主要有三种IO模型,分别是阻塞IO(BIO)、非阻塞IO(NIO)和 异步IO(AIO)。Java中...

  • Java I/O 1.1

    Java IO一般大家常说的IO分为两个部分: 1.java.io包中堵塞型IO(BIO);2.java.nio包...

  • Java网络编程:Netty框架学习(二)---Java NIO

    概述 上篇中已经讲到Java中的NIO类库,Java中也称New IO,类库的目标就是要让Java支持非阻塞IO,...

  • 从源码设计角度理解Java IO

    概述 Java的核心库java.io提供了全面的IO接口。包括:文件读写、标准设备输出等。Java中IO是以流为基...

  • NIO与BIO的区别

    BIO的概念又称Java IO,是java.io包中的InputStream、OutputStream这种同步、阻...

  • Java知识梳理三

    一、Java中的IO 1.IO的概述 Java IO方式有很多种,基于不同的IO抽象模型和交互方式,可以进行...

  • Java IO & NIO & NIO2

    概览 IO是Java中的最重要的一个部分. 其中, java.io是所有编程者都应该掌握的IO方式. 在Java ...

网友评论

    本文标题:Java 中的IO

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