Java复习第十一天学习笔记(IO流),附有道云笔记链接

news/2024/5/18 23:14:10/文章来源:https://blog.csdn.net/m0_62220699/article/details/137089687

【有道云笔记】十一 3.27 IO流
https://note.youdao.com/s/PeEdd3Zo

一、IO介绍以及分类

IO: Input Output

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

IO流的分类:

1、 根据处理的数据类型不同可以分为:字符流和字节流。

2、根据数据的流向不同可以分为:输入流和输出流。

字节流(Byte Stream):

  1. 字节流以字节为单位进行读取和写入操作,适合处理二进制数据(如图片、视频、音频等)或者文本文件。
  2. 字节流类通常以 InputStream 和 OutputStream 结尾,例如 FileInputStream、FileOutputStream。
  3. 字节流可以用于读写任何类型的文件,但对于文本文件的处理可能需要做字符编码转换。

字符流(Character Stream):

  1. 字符流以字符为单位进行读取和写入操作,适合处理文本数据。字符流会自动处理字符编码转换,避免了字节流在处理文本时可能出现的乱码问题。
  2. 字符流类通常以 Reader 和 Writer 结尾,例如 FileReader、FileWriter。
  3. 字符流适合处理文本文件,能够更方便地读写文本中的字符数据。

选择建议:

  1. 如果你需要处理文本文件,推荐使用字符流,因为它们能够更好地处理字符编码和文本数据。
  2. 如果需要处理二进制文件或者未经处理的数据,应该使用字节流。

0

0

二、字符流

@Test public void test1() { try { FileReader fileReader = new FileReader("io.txt"); int ch1 = fileReader.read(); System.out.println((char)ch1);//a int ch2 = fileReader.read(); System.out.println((char)ch2);//b int ch3 = fileReader.read(); System.out.println((char)ch3);//c int ch4 = fileReader.read(); System.out.println(ch4);//-1 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void test2() { try { FileReader fileReader = new FileReader("io.txt"); int ch = -1; //while循环去读,直到ch的值等于-1,就退出循环 while ((ch = fileReader.read()) != -1) { System.out.println((char)ch); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void test3() { FileReader fileReader = null; try { fileReader = new FileReader("io.txt"); char[] buffer = new char[10]; int length = -1; //public int read(char[] cbuf) //将字符读入数组 //返回:读取的字符数,如果已到达流的末尾,则返回 -1 while ((length = fileReader.read(buffer)) != -1) { System.out.println(length); System.out.println(buffer); } System.out.println(length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void test4() { FileReader fileReader = null; FileWriter fileWriter = null; try { fileReader = new FileReader("io.txt"); fileWriter = new FileWriter("io_back.txt"); char[] buffer = new char[10]; int length = -1; while ((length = fileReader.read(buffer)) != -1) { System.out.println(length); System.out.println(buffer); //读出多少就写多少,最后一次读出的数据很有可能不够buffer数组长度的数据 fileWriter.write(buffer, 0, length); } System.out.println(length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }

三、字节流

@Test public void testFileInputStreamFileOutputStram() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream("bd.png"); fileOutputStream = new FileOutputStream("bd_back.png"); byte[] buffer = new byte[1024]; int length = -1; while ((length = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, length); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }

四、对象流

ObjectInputStream、ObjectOutputStream

将对象写入文件的操作流ObjectOutputStream,称为序列化。

从流中读取对象的操作流程ObjectInputStream,称为反序列化。

java.io.NotSerializableException: com.situ.day13.Student

Serialize序列化

@Test public void testObjectOutputStream() { Student student = new Student(); student.setId(1); student.setName("zhangsan"); student.setAge(23); student.setGender("男"); ObjectOutputStream objectOutputStream = null; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("stu"); objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(student); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOutputStream != null) { try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testObjectInputStream() { ObjectInputStream objectInputStream = null; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream("stu"); objectInputStream = new ObjectInputStream(fileInputStream); Student student = (Student) objectInputStream.readObject(); System.out.println(student); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }

一般情况下:先打开的后关闭,后打开的先关闭。

另一种情况:看依赖关系,如果流A依赖于流B,先关闭流A,再关闭流B。

五、IO异常的原因

在使用字节流读取文件时,可能会遇到各种 I/O 异常情况。以下是一些常见的导致 I/O 异常的情况:

  1. 文件不存在:尝试读取一个不存在的文件会导致 FileNotFoundException。
  2. 无法访问文件:权限不足或文件被其他程序占用可能导致 AccessDeniedException。
  3. 文件路径错误:如果提供的文件路径有误,会导致 InvalidPathException 或 InvalidParameterException。
  4. 磁盘空间不足:在写入文件时,磁盘空间不足会导致 DiskFullException。
  5. 网络异常:当从网络位置读取文件时,网络连接中断或超时会导致 SocketTimeoutException 或 IOException。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_1033550.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

无人机编队 | 基于自适应航迹评价函数权重的动态窗口法长机-僚机法实现多无人机路径规划附matlab代码

基本概述 实现基于自适应航迹评价函数权重的动态窗口法(Dynamic Window Approach, DWA)的长机-僚机(Leader-Follower)多无人机路径规划是一个复杂的任务,涉及到多个算法的组合与改进。这里我会简要介绍其原理,并提供一个基础的Matlab代码框架,但请注意,这只是一个起点…

如何恢复被.locked勒索病毒加密的服务器和数据库?

.locked勒索病毒有什么特点? .locked勒索病毒的特点主要包括以下几个方面: 文件加密:.locked勒索病毒会对受感染设备上的所有文件进行加密,包括图片、文档、视频和其他各种类型的重要文件。一旦文件被加密,文件的扩展…

Day14_学点CSS_高级选择器Demo

1 后代选择器s1 s2 <!--~ 适度编码益脑&#xff0c;沉迷编码伤身&#xff0c;合理安排时间&#xff0c;享受快乐生活。~ Copyright TangXJ~ Created by TangXJ~ Created&Used date: 2024/3/29 下午3:46 ~ 2024/3/29 下午3:47~ Modified date: 2024/3/29 下午3:47-->…

C语言 | Leetcode C语言题解之3题无重复字符的最长子串

题目&#xff1a; 题解&#xff1a; int lengthOfLongestSubstring(char * s) {//类似于hash的思想//滑动窗口维护int left 0;int right 0;int max 0;int i,j;int len strlen(s);int haveSameChar 0;for(i 0; i < len ; i ){if(left < right){ //检测是否出现重…

OpenHarmony实战开发-图案密码锁组件的使用

介绍 本示例展示了图案密码锁组件的使用&#xff0c;实现了密码设置、验证和重置功能。 图案密码锁组件&#xff1a;以宫格图案的方式输入密码&#xff0c;用于密码验证。手指触碰图案密码锁时开始进入输入状态&#xff0c;手指离开屏幕时结束输入状态并向应用返回输入的密码…

STM32学习和实践笔记(4): 分析和理解GPIO_InitTypeDef GPIO_InitStructure (b)

继续上篇博文&#xff1a;STM32学习和实践笔记&#xff08;4&#xff09;: 分析和理解GPIO_InitTypeDef GPIO_InitStructure (a)-CSDN博客 往下写&#xff0c; 为什么&#xff1a;当GPIO_InitStructure.GPIO_PinGPIO_Pin_0 ; 时&#xff0c;其实就是将对应的该引脚的寄存器地…

docker--部署 (超详版) (五)

环境准备&#xff1a;docker&#xff0c;mysql&#xff0c;redis&#xff0c;镜像&#xff0c;nginx 把虚拟机打开&#xff0c;连接xshell&#xff0c;参考博客&#xff1a; https://blog.csdn.net/m0_74229802/article/details/136965820?spm1001.2014.3001.5501 一&#x…

HCIP作业4

实验步骤&#xff1a; 第一步 给PC1和PC2和PC3配地址 第二步给R1到R5配置接口IP地址 [R1]int g0/0/0 [R1-GigabitEthernet0/0/0]ip ad 192.168.1.254 24 R1&#xff1a;[R1-GigabitEthernet0/0/0]int s4/0/0 [R1-Serial4/0/0]ip ad 15.1.1.1 24 [R1-Serial4/0/0]dis ip in…

Flutter 开发学习笔记(0):环境配置

文章目录 前言开发需求环境配置运行出现问题我运行也是解决了很久的问题镜像源设置为清华的镜像源&#xff08;不知道有没有影响&#xff09;使用JDK17&#xff0c;测试过JDK21和JDK11都不行手动下载flutter 对应的gradle添加阿里云代理安卓编译下载 运行成功&#xff01; 前言…

基于Springboot的一站式家装服务管理系统(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的一站式家装服务管理系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体…

2024年MathorCup数学建模思路A题B题C题D题思路分享

文章目录 1 赛题思路2 比赛日期和时间3 组织机构4 建模常见问题类型4.1 分类问题4.2 优化问题4.3 预测问题4.4 评价问题 5 建模资料 1 赛题思路 (赛题出来以后第一时间在CSDN分享) https://blog.csdn.net/dc_sinor?typeblog 2 比赛日期和时间 报名截止时间&#xff1a;2024…

css设置文字铺满盒子

<div>收货人</div>&#xff1a; <div>电话</div>&#xff1a; <div>省市区</div>&#xff1a; width: 100rpx;border: 1px solid rebeccapurple;display: inline-block;text-align-last: justify;

构建安全高效的用户登录系统:登录流程设计与Token验证详解

在当今数字化时代&#xff0c;用户登录系统是几乎所有在线服务的基础。然而&#xff0c;随着网络安全威胁的不断增加&#xff0c;设计一个安全可靠的登录系统变得至关重要。本文将深入探讨用户登录流程的设计原则以及Token验证的实现方式&#xff0c;带您了解如何构建安全高效的…

网络安全新视角:数据可视化的力量

在当今数字化时代&#xff0c;网络安全已成为各大企业乃至国家安全的重要组成部分。随着网络攻击的日益复杂和隐蔽&#xff0c;传统的网络安全防护措施已难以满足需求&#xff0c;急需新型的解决方案以增强网络防护能力。数据可视化技术&#xff0c;作为一种将复杂数据转换为图…

代码+视频,手动绘制logistic回归预测模型校准曲线(Calibration curve)(1)

校准曲线图表示的是预测值和实际值的差距&#xff0c;作为预测模型的重要部分&#xff0c;目前很多函数能绘制校准曲线。 一般分为两种&#xff0c;一种是通过Hosmer-Lemeshow检验&#xff0c;把P值分为10等分&#xff0c;求出每等分的预测值和实际值的差距. 另外一种是calibra…

基于Websocket的局域网聊天系统

1.1 研究背景及意义 本项目所对应领域的研究背景及意义[1]。新冠肺炎局域网通信发生以来&#xff0c;大数据、云计算、人工智能等新一代信息技术加速与交通、局域网通信、教育、金融等领域深度融合&#xff0c;让局域网通信防控的组织和执行更加高效&#xff0c;成为战“疫”的…

【三】EMQX 手动创建集群

EMQX 手动创建集群 简介 因为项目中使用到了emqx中间件&#xff0c;所以近期对中间件进行了进一步的研究&#xff0c;每次选用中间件我都会考虑可用性方案&#xff0c;如下是本地实践的记录。 一、部署 1、创建一个 Docker 网络&#xff0c;用于节点间通信。处于同一网络下的…

STM32F103通过labview上位机上传温湿度数据到OneNET物联网平台

资料下载地址&#xff1a;STM32F103通过labview上位机上传温湿度数据到OneNET物联网平台 本实验通过两个STM32单片机设备分别测量室内外的温湿度&#xff0c;并把数据发送到上位机上传到ONENET物联网平台。 大体数据传输流程如下&#xff1a; 首先是注册OneNET平台账号&#…

【面试题】RocketMQ怎么处理消息积压?

如图,消息积压主要是因为&#xff0c;消费能力不足&#xff1a; 在RocketMQ中&#xff0c;处理消息积压的方法可以采取以下几种策略&#xff1a; 增加消费者数量&#xff1a;可以通过增加消费者数量来提高消息的消费速度。通过增加消费者实例或者消费者组的数量&#xff0c;可…

新网站秒收录技术,新网站百度收录时间

在建立新网站后&#xff0c;让它尽快被搜索引擎收录是网站主最为关注的事情之一。百度作为中国最大的搜索引擎&#xff0c;网站被其快速收录对于增加曝光和流量至关重要。本文将介绍一些新网站秒收录技术&#xff0c;以及一般情况下新网站被百度收录需要的时间。 新网站秒收录技…