java的IO流

2020-05-07  本文已影响0人  小石读史

根据传输数据的类型,分为字节流和字符流,根据流向的不同分为输入流和输出流。

以下是JAVAIO流框架:


image.png

从图中可以看出,以Stream结尾的为字节流,以Writer或者Reader结尾的为字符流。所有的输入流都是抽象类IuputStream(字节输入流)或者抽象类Reader(字符输入流)的子类,所有的输出流都是抽象类OutputStream(字节输出流)或者抽象类Writer(字符输出流)的子类。

1、字符流FileReader和FileWriter
使用FileReader和FileWriter类完成文本文件复制
FileReader


image.png
public class TestFileReader {
    public static void main(String[] args)throws IOException {
        FileReader fileReader = new FileReader("F:\\daren.txt");
        char[] buffer = new char[32];
        int hasRead = 0;
        while((hasRead = fileReader.read(buffer)) > 0){
            System.out.print(new String(buffer, 0, hasRead));
        }
        fileReader.close();
    }
}

FileWriter


image.png
public class FileWriterTest {
    public static void main(String[] args)throws IOException {
        File result = new File("F:\\静夜思.txt");
        FileWriter fileWriter = new FileWriter(result);
        fileWriter.write("床前明月光,\r\n");
        fileWriter.write("疑是地上霜.\r\n");
        fileWriter.close();
    }
}

2、BufferedReader和BufferedWriter
BufferedReader和BufferedWriter出现的目的是为了对FileReader以及FileWriter的读写操作进行增强。
BufferedReader和BufferedWriter类各拥有8192个字符的缓冲区。当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并放满缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取。如果缓冲区数据不足,才会再从文件中读取,使用BufferedWriter时,写入的数据并不会先输出到目的地,而是先存储至缓冲区中。如果缓冲区中的数据满了,才会一次对目的地进行写出。


image.png
image.png
public class BufferedReaderWriterTest {
    public static void main(String[] args) {
        //高效缓冲输入流
        BufferedReader reader = null;
        try {
            //传入的是reader的子类
            reader = new BufferedReader(new FileReader("F:\\静夜思.txt"));
            String line;
            //如果还有下一行就一直读下去readLine()用于读取一行
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //高效缓冲输出流
        BufferedWriter writer = null;
        BufferedReader reader1;
        try {
            //创建高效缓冲输出流
            writer = new BufferedWriter(new FileWriter("F:\\静夜思2.txt"));
            //创建高效缓冲输入流
            reader1 = new BufferedReader(new FileReader("F:\\静夜思.txt"));
            String line;
            while ((line = reader1.readLine()) != null) {
                writer.write(line);
                writer.newLine();
                writer.flush();

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3、FileInputStream和FileOutputStream


image.png
image.png
public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        FileInputStream fin = new FileInputStream("F:\\学习机.jpg");
        FileOutputStream fout = new FileOutputStream("F:\\学习机2.jpg");
        int len = 0;
        byte[] buff = new byte[1024];
        while ((len = fin.read(buff)) != -1) {
            fout.write(buff, 0, len);
        }
        fin.close();
        fout.close();
    }
}

4、BufferedInputStream&BufferedOutputStream
java IO流的继承关系如下
InputStream
|_FileInputStream
|_BufferedInputStream
BufferedInputStream 是套在其他InputStream之外的,是用来优化里面的InputStream的性能的,FileInputStream是读文件的实现,所以通常使用BufferedInputStream&BufferedOutputStream作为FileInputStream&FileOutputStream的外包装来优化读写的性能。

public class BufferedInputStreamTest {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bfin = new BufferedInputStream(new FileInputStream("F:\\学习机.jpg"));
        BufferedOutputStream bfout = new BufferedOutputStream(new FileOutputStream("F:\\学习机3.jpg"));
        int len = 0;
        byte[] buff = new byte[1024];
        while ((len = bfin.read(buff)) != -1) {
            bfout.write(buff, 0, len);
        }
        bfin.close();
        bfout.close();
    }
}
上一篇下一篇

猜你喜欢

热点阅读