您的位置 首页 java

原来这就是 Java IO 啊····

本文分享自华为云社区《惊呆了,原来JavaIO如此简单》,原文作者:香菜聊游戏。

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

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

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

类结构如下

在平常的读写文件的时候可以先用基本流,然后看是否需要字符流,最后在用上带 buffer 的流。IO 流的设计思想就是装饰器模式,一层一层的进行升级功能。

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

 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)

 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、获取键盘输入

 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 三个关键词记住

点击关注,第一时间了解华为云新鲜技术~

文章来源:智云一二三科技

文章标题:原来这就是 Java IO 啊····

文章地址:https://www.zhihuclub.com/176046.shtml

关于作者: 智云科技

热门文章

评论已关闭

1条评论

  1. Dear valued customers,

    Are you looking to increase conversions on your website? One proven way to do this is by using social proof. Our social proof widget for websites makes it easy for you to display real-time user activity, such as product purchases or user reviews, to your visitors. This helps build trust and credibility, leading to higher conversions and sales.

    Our widget is easy to install and customize, and we offer excellent support to ensure that you get the most out of it. Plus, with a risk-free 30-day trial, you can see for yourself how it can benefit your business.

    Don’t miss out on this opportunity to boost your website’s performance. Try our social proof widget today and start seeing results.

    Best regards,

网站地图