学点儿Java_Day12_IO流
1 IO介绍以及分类
IO: Input Output
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作
1.1 IO流的分类
1. 根据处理的数据类型不同可以分为:字符流和字节流。
2. 根据数据的流向不同可以分为:输入流和输出流。
1.2 字节流(Byte Stream)
1.字节流以字节为单位进行读取和写入操作,适合处理二进制数据(如图片、视频、音频等)或者文本文件。
2.字节流类通常以 InputStream 和 OutputStream 结尾,例如 FileInputStream、FileOutputStream。
3.字节流可以用于读写任何类型的文件,但对于文本文件的处理可能需要做字符编码转换。
1.3 字符流(Character Stream)
1.字符流以字符为单位进行读取和写入操作,适合处理文本数据。字符流会自动处理字符编码转换,避免了字节流在处理文本时可能出现的乱码问题。
2.字符流类通常以 Reader 和 Writer 结尾,例如 FileReader、FileWriter。
3.字符流适合处理文本文件,能够更方便地读写文本中的字符数据。
1.4 选择建议
1.如果你需要处理文本文件,推荐使用字符流,因为它们能够更好地处理字符编码和文本数据。
2.如果需要处理二进制文件或者未经处理的数据,应该使用字节流。
2 字符流
@Testpublic void test1() {FileReader fileReader = null;//声明放在外边 局部变量必须初始化//FileReader fileReader;//声明放在外边 ×××××try {fileReader = new FileReader("io.txt");//打开水龙头//Reads a single character.int ch1 = fileReader.read();System.out.println((char)ch1);//aint ch2 = fileReader.read();System.out.println((char)ch2);//bint ch3 = fileReader.read();System.out.println((char)ch3);//cint ch4 = fileReader.read();System.out.println((char)ch4);//System.out.println(ch4);//-1} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//在finally里边必须关闭IO流try {fileReader.close();} catch (IOException e) {throw new RuntimeException(e);}}}@Testpublic void test2() {try {FileReader fileReader = new FileReader("io.txt");int ch = -1;while ((ch = fileReader.read()) != -1) {System.out.println((char)ch);}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}//----------Day12---------------@Testpublic void test3() {try {FileReader fileReader = new FileReader("io.txt");char[] buffer = new char[10];int length = -1;//public int read(char[] cbuf)//一次将10个字符读入到buffer数组里面//返回:读取的字符数,如果已经达到流的末尾,返回-1while ((length = fileReader.read(buffer)) != -1) {System.out.println(length);System.out.println(buffer);}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}@Testpublic 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;//public int read(char[] cbuf)//一次将10个字符读入到buffer数组里面//返回:读取的字符数,如果已经达到流的末尾,返回-1while ((length = fileReader.read(buffer)) != -1) {System.out.println(length);System.out.println(buffer);//读出多少写多少,最后一次读出来的数据很有可能不够buffer数组的长度fileWriter.write(buffer, 0, length);}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);} finally {//先打开的后关闭if (fileWriter != null) {try {fileWriter.close();} catch (IOException e) {throw new RuntimeException(e);}}if (fileReader != null) {try {fileReader.close();} catch (IOException e) {throw new RuntimeException(e);}}}}
3 字节流
@Testpublic void test5() {//FileInoutSteam 父类是InputSteamFileInputStream fileInputStream = null;//经验 先生命成null 局部变量必须初始化FileOutputStream fileOutputStream = null;try {fileInputStream = new FileInputStream("baidu.png");fileOutputStream = new FileOutputStream("baidu_back.png");byte[] buffer = new byte[1024];int length = -1;while ((length = fileInputStream.read(buffer)) != -1){//赋值语句有结果 结果就是length值System.out.println(length);//fileOutputStream.write(buffer, 0, length);//这个不会多写fileOutputStream.write(buffer);//会多写几个字节}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);} finally {if (fileOutputStream != null) {try {fileOutputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}}
4 对象流
ObjectInputStream、ObjectOutputStream
将对象写入文件的操作流ObjectOutputStream,称为序列化。
从流中读取对象的操作流程ObjectInputStream,称为反序列化。
java.io.NotSerializableException: com.situ.day13.Student
Serialize:序列化
/** 适度编码益脑,沉迷编码伤身,合理安排时间,享受快乐生活。* Copyright @TangXJ* Created by TangXJ* Created&Used date: 2024/3/27 下午2:06 ~ 2024/3/27 下午3:39* Modified date: 2024/3/27 下午3:39*/package com.sdust.day12;import org.junit.Test;import java.io.*;public class Student implements Serializable {//实现一个接口private Integer id;private String name;private Integer age;private String gender;public Student() {}public Student(Integer id, String name, Integer age, String gender) {this.id = id;this.name = name;this.age = age;this.gender = gender;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +'}';}//对象流@Testpublic void test1() {Student student = new Student();student.setId(1);student.setName("zhangsan");student.setAge(23);student.setGender("男");//java.lang.RuntimeException: java.io.NotSerializableException: com.sdust.day12.Student//不能序列化的异常//实现一个接口ObjectOutputStream objectOutputStream = null;//不负责写文件 对象转换交给它ObjectOutputStream来完成//负责把一个对象 做一个转换FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream("stu");objectOutputStream = new ObjectOutputStream(fileOutputStream);objectOutputStream.writeObject(student);} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);} finally {//x先打开的后关闭if (objectOutputStream != null){try {objectOutputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}if (fileOutputStream != null) {try {fileOutputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}}@Testpublic void test2() {ObjectInputStream objectInputStream = null;FileInputStream fileInputStream = null;try {fileInputStream = new FileInputStream("stu");objectInputStream = new ObjectInputStream(fileInputStream);//Student student = objectInput.readObject();//报错 父类不能转换为子类//Object object = new Student();Student student = (Student) objectInputStream.readObject();System.out.println(student);} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);} catch (ClassNotFoundException e) {throw new RuntimeException(e);} finally {if (objectInputStream != null) {try {objectInputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}}
}
这个Demo运行时要注释掉所有构造函数。
一般情况下:先打开的后关闭,后打开的先关闭。
另一种情况:看依赖关系,如果流A依赖于流B,先关闭流A,再关闭流B。
相关文章:

学点儿Java_Day12_IO流
1 IO介绍以及分类 IO: Input Output 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据…...

【python】python编程初探1----python中的基本语法,标识符,关键字,注释,多行书写,代码缩进,语句块,模块等
欢迎来CILMY23的博客喔,本篇为【python】python编程初探1----python中的基本语法,标识符,关键字,注释,多行书写,代码缩进,语句块,模块,感谢观看,支持的可以给…...
牛客周赛 Round 38
牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com) A-小红的正整数自增_牛客周赛 Round 38 (nowcoder.com) 取出最后一位判断即可 #include<iostream> #include<algorithm> #include<vector> #include<set> #include…...
漏洞扫描操作系统识别技术原理
漏洞扫描过程中,操作系统识别技术是至关重要的一步,因为它有助于扫描器针对性地选择适用的漏洞检测规则,提高扫描的准确性和效率。以下是漏洞扫描中操作系统识别技术的主要原理: **1. **TCP/IP 协议栈指纹识别** **原理**&#…...
数据结构与算法-分治算法
数据结构与算法 数据结构与算法是计算机科学中的两个核心概念,它们在软件开发和问题解决中起着至关重要的作用。 数据结构 数据结构是计算机中存储、组织和管理数据的方式,它能够帮助我们高效地访问和修改数据。不同的数据结构适用于不同类型的应用场…...
MNN详细介绍、安装和编译
目录 第一章:MNN简介 1.1、MNN概述 1.2、MNN特点 1.3、MNN在移动端的应用优势 第二章:MNN安装与配置 2.1、环境准备 2.2、下载与编译 第三章:MNN使用指南 3.1、模型转换与部署 3.2、推理示例 第四章:MNN高级应用与优化技巧...

uniapp-Form示例(uviewPlus)
示例说明 Vue版本:vue3 组件:uviewPlus(Form 表单 | uview-plus 3.0 - 全面兼容nvue的uni-app生态框架 - uni-app UI框架) 说明:表单组建、表单验证、提交验证等; 截图: 示例代码 <templat…...

【Linux】详解进程程序替换
一、替换原理 用fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个程序。当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程开始执…...

vue中使用jsmind生成脑图
项目部分参数: vue:2.6.10 node:16.20.0 1、使用命令行安装jsmind: npm i jsmind -S 2、在文件中引入jsmind,并编写渲染jsmind的代码:: <template><!-- jsmind容器 --><divid"jsmi…...

yarn按包的时候报错 ../../../package.json: No license field
运行 yarn config list 然后运行 yarn config set strict-ssl false 之后yarn就成功了...

【Python从入门到进阶】51、电影天堂网站多页面下载实战
接上篇《50、当当网Scrapy项目实战(三)》 上一篇我们讲解了使用Scrapy框架在当当网抓取多页书籍数据的效果,本篇我们来抓取电影天堂网站的数据,同样采用Scrapy框架多页面下载的模式来实现。 一、抓取需求 打开电影天堂网站&…...

苹果CMS影视APP源码,二开版本带视频教程
编译app教程 工具下载:Android Studio 官网地址:https://developer.android.google.cn/studio/ 环境设置: 设置中文:https://blog.csdn.net/qq_37131111/article/details/131492844 汉化包找最新的下载就行了,随便下载…...
Zigbee技术在智能农业领域的应用研究
Zigbee技术在智能农业领域的应用研究 **摘要:**随着现代信息技术的飞速发展,智能农业已成为当今农业发展的新趋势。Zigbee技术作为一种低功耗、低成本的无线通信技术,在智能农业领域具有广泛的应用前景。本文深入分析了Zigbee技术的原理和特…...
Spring Cloud Gateway 中GET请求能正常访问,POST请求出现Unable to handle DataBuffer
报错信息如下: java.lang.IllegalArgumentException: Unable to handle DataBuffer of type class org.springframework.http.server.reactive.UndertowServerHttpRequest$UndertowDataBufferat org.springframework.cloud.gateway.filter.NettyRoutingFilter.getB…...
什么是git? 初步认识git 如何使用git
Git是什么? Git 是分布式版本控制系统,可以有效,高速地处理从很小到非常大地项目版本管理,分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者可以通过克隆,在本地机器上拷贝一个完整的Git …...

Douyin视频详情数据API接口(视频详情,评论)
抖音官方并没有直接提供公开的视频详情数据采集API接口给普通用户或第三方开发者。抖音的数据采集通常受到严格的限制,以保护用户隐私和平台安全。 请求示例,API接口接入Anzexi58 如果您需要获取抖音视频详情数据,包括评论、点赞等ÿ…...

MySQL 索引:索引为什么使用 B+树?
Hash 索引不支持顺序和范围查询; 二叉查找树(BST):解决了排序的问题,极端情况下可能会退化成线性链表,查询效率急剧下降; 平衡二叉树(AVL) :通过旋转解决了平衡的问题,但是旋转操作效率太低&am…...
2024年第四届天府杯全国大学生数学建模竞赛B题思路
B题:新质生产力引领下的企业生产与发展策略优化 问题背景 随着技术的飞速发展,新质生产力如人工智能、大数据分析、物联网等技术被广泛应用于生产和服务过程中,极大地提高了生产效率和产品质量,改变了传统的生产与经营模式。一家…...
c++部分题
const关键字与宏定义的区别是什么? const关键字和宏定义在功能上有相似之处,但在实现和使用上有很大的区别。 作用域和类型安全性: const关键字定义的常量具有作用域和类型安全性。它们的作用域仅限于声明它们的块,并且在编译时会…...
验证回文串
如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。 字母和数字都属于字母数字字符。 给定一个字符串 s,如果它是 回文串 ,返回 true ;否则&#…...
STM32+rt-thread判断是否联网
一、根据NETDEV_FLAG_INTERNET_UP位判断 static bool is_conncected(void) {struct netdev *dev RT_NULL;dev netdev_get_first_by_flags(NETDEV_FLAG_INTERNET_UP);if (dev RT_NULL){printf("wait netdev internet up...");return false;}else{printf("loc…...
Leetcode 3577. Count the Number of Computer Unlocking Permutations
Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接:3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁&#x…...

Vue2 第一节_Vue2上手_插值表达式{{}}_访问数据和修改数据_Vue开发者工具
文章目录 1.Vue2上手-如何创建一个Vue实例,进行初始化渲染2. 插值表达式{{}}3. 访问数据和修改数据4. vue响应式5. Vue开发者工具--方便调试 1.Vue2上手-如何创建一个Vue实例,进行初始化渲染 准备容器引包创建Vue实例 new Vue()指定配置项 ->渲染数据 准备一个容器,例如: …...
css的定位(position)详解:相对定位 绝对定位 固定定位
在 CSS 中,元素的定位通过 position 属性控制,共有 5 种定位模式:static(静态定位)、relative(相对定位)、absolute(绝对定位)、fixed(固定定位)和…...

ardupilot 开发环境eclipse 中import 缺少C++
目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...

零基础设计模式——行为型模式 - 责任链模式
第四部分:行为型模式 - 责任链模式 (Chain of Responsibility Pattern) 欢迎来到行为型模式的学习!行为型模式关注对象之间的职责分配、算法封装和对象间的交互。我们将学习的第一个行为型模式是责任链模式。 核心思想:使多个对象都有机会处…...
3403. 从盒子中找出字典序最大的字符串 I
3403. 从盒子中找出字典序最大的字符串 I 题目链接:3403. 从盒子中找出字典序最大的字符串 I 代码如下: class Solution { public:string answerString(string word, int numFriends) {if (numFriends 1) {return word;}string res;for (int i 0;i &…...

零基础在实践中学习网络安全-皮卡丘靶场(第九期-Unsafe Fileupload模块)(yakit方式)
本期内容并不是很难,相信大家会学的很愉快,当然对于有后端基础的朋友来说,本期内容更加容易了解,当然没有基础的也别担心,本期内容会详细解释有关内容 本期用到的软件:yakit(因为经过之前好多期…...
Mobile ALOHA全身模仿学习
一、题目 Mobile ALOHA:通过低成本全身远程操作学习双手移动操作 传统模仿学习(Imitation Learning)缺点:聚焦与桌面操作,缺乏通用任务所需的移动性和灵活性 本论文优点:(1)在ALOHA…...
Java线上CPU飙高问题排查全指南
一、引言 在Java应用的线上运行环境中,CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时,通常会导致应用响应缓慢,甚至服务不可用,严重影响用户体验和业务运行。因此,掌握一套科学有效的CPU飙高问题排查方法&…...