Java知识123

Java 中的IO

2017-06-21  本文已影响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 操作转移回操作系统,因而可以极大地提高速度。
其中重要的几个概念:

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

上一篇 下一篇

猜你喜欢

热点阅读