好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

带你快速搞定java IO

一、IO底层是怎么回事?

操作系统就是管家,电脑的设备就是资源,如果进程先要操作资源,必须要进行系统调用,有操作系统去处理,然后再返回给进程,这样的代理模式是不是很常见?因此app 就是你写的程序,资源就是硬盘或者其他的设备,io就是进行的系统调用。

为了保证操作系统的稳定性和安全性,一个进程的地址空间划分为 用户空间(User space) 和 内核空间(Kernel space ) 。像我们平常运行的应用程序都是运行在用户空间,只有内核空间才能进行系统态级别的资源有关的操作,比如如文件管理、进程通信、内存管理等等。也就是说,我们想要进行 IO 操作,一定是要依赖内核空间的能力。并且,用户空间的程序不能直接访问内核空间。当想要执行 IO 操作时,由于没有执行这些操作的权限,只能发起系统调用请求操作系统帮忙完成。因此,用户进程想要执行 IO 操作的话,必须通过 系统调用 来间接访问内核空间

二、梳理类的结构

java的io 实在太复杂了,往往新手很难掌握,因为只缘身在此山中,新手往往很难从全体去看到问题的本质,我和打铁的朋友的聊天截图能帮你解答一些。

类结构如下

在平常的读写文件的时候可以先用基本流,然后看是否需要字符流,最后在用上带buffer 的流。

IO流的设计思想就是装饰器模式,一层一层的进行升级功能。

三、IO类大点兵

四、来波实例展示

1、访问操作文件(FileInputStream/FileReader ,FileOutputStream/FileWriter)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

/**

* 拷贝文件

* @author 香菜

*/

public class CopyFileWithStream {

    public static void main(String[] args) {

        int b = 0 ;

        String inFilePath = "D:\\wechat\\A.txt" ;

        String outFilePath = "D:\\wechat\\B.txt" ;

        try (FileInputStream in = new FileInputStream(inFilePath); FileOutputStream out= new FileOutputStream(outFilePath)) {

            while ((b = in.read()) != - 1 ) {

                out.write(b);

           }

       } catch (IOException e) {

            e.printStackTrace();

       }

        System.out.println( "文件复制完成" );

   }

}

2、缓存流的使用(BufferedInputStream/BufferedOutputStream,BufferedReader/BufferedWriter)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package org.pdool.iodoc;

import java.io.*;

/**

* 拷贝文件

*

* @author 香菜

*/

public class CopyFileWithBuffer {

    public static void main(String[] args) throws Exception {

        String inFilePath = "D:\\wechat\\A.txt" ;

        String outFilePath = "D:\\wechat\\B.txt" ;

        try (BufferedInputStream bis = new BufferedInputStream( new FileInputStream(inFilePath));

              BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(outFilePath))) {

            byte [] b = new byte [ 1024 ];

            int off = 0 ;

            while ((off = bis.read(b)) > 0 ) {

                bos.write(b, 0 , off);

           }

       }

   }

}

3、获取键盘输入

?

1

2

3

4

5

6

7

8

9

import java.util.Scanner;

public class TestScanner {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextLine()){

            System.out.println(scanner.nextLine());

       }

   }

}

让我们看下源码是啥情况:

总结:

而Reader/Writer则是用于操作字符,增加了字符编解码等功能,适用于类似从文件中读取或者写入文本信息。本质上计算机操作的都是字节,不管是网络通信还是文件读取,Reader/Writer相当于构建了应用逻辑和原始数据之间的桥梁。

Buffered等带缓冲区的实现,可以避免频繁的磁盘读写,进而提高IO处理效率。

记住IO流的设计模式是装饰器模式,对流进行功能升级。

stream , reader , buffered 三个关键词记住

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://gamwatcher.blog.csdn.net/article/details/117094092

查看更多关于带你快速搞定java IO的详细内容...

  阅读:17次