阿里云建站论坛网站/深圳网络推广服务是什么
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 ;否则&#…...

vue2高德地图选点
<template><el-dialog :title"!dataForm.id ? 新建 : isDetail ? 详情 : 编辑" :close-on-click-modal"false" :visible.sync"show" class"rv-dialog rv-dialog_center" lock-scroll width"74%" :before-close&q…...

Gitflow:一种依据 Git 构建的分支管理工作流程模式
文章目录 前言Gitflow 背景Gitflow 中的分支模型Gitflow 的版本号管理简单模拟 Gitflow 工作流 前言 Gitflow 工作流是一种版本控制流程,主要适用于较大规模的团队。这个流程在团队中进行合作时可以避免冲突,并能快速地完成项目,因此在很多软…...

利用云手机技术,开拓海外社交市场
近年来,随着科技的不断进步,云手机技术逐渐在海外社交营销领域崭露头角。其灵活性、成本效益和全球性特征使其成为海外社交营销的利器。那么,究竟云手机在海外社交营销中扮演了怎样的角色呢? 首先,云手机技术能够消除地…...

脚本实现Ubuntu设置屏幕无人操作,自动黑屏
使用 xrandr 命令可以实现对屏幕的控制,包括调整分辨率、旋转屏幕以及关闭屏幕等。要实现 Ubuntu 设置屏幕在无人操作一段时间后自动黑屏,非待机,并黑屏后点击触摸屏可以唤醒屏幕,可以借助 xrandr 命令来实现。 首先,…...

16.JRE和JDK
程序员在编写代码的时候其实是需要一些环境,例如我们之前写的HelloWorld。我们需要的东西有JVM、核心类库、开发工具。 1、JVM(Java Virtual Machine):Java虚拟机,真正运行Java程序的地方。没有虚拟机,代码…...

C++从入门到精通——命名空间
命名空间 前言一、命名空间引例什么是命名空间 二、命名空间定义正常的命名空间定义嵌套的命名空间多个相同名称的命名空间 三、命名空间使用加命名空间名称及作用域限定符使用using将命名空间中某个成员引入使用using namespace 命名空间名称引用引用命名空间和引用头文件有什…...

JAVA面试大全之JAVA新特性篇
目录 1、Java 8特性 1.1、什么是函数式编程?Lambda表达式? 1.2、Stream中常用方法? 1.3、什么是FunctionalInterface? 1.4、如何自定义函数接口?...

【ZZULIOJ】1008: 美元和人民币(Java)
目录 题目描述 输入 输出 样例输入 Copy 样例输出 Copy code 题目描述 美元越来越贬值了,手上留有太多的美元似乎不是件好事。赶紧算算你的那些美元还值多少人民币吧。假设美元与人民币的汇率是1美元兑换6.5573元人民币,编写程序输入美元的金额&a…...

LeetCode刷题笔记之动态规划(三)
一、子序列/子数组问题 子序列:按原数组的顺序排列,不一定是原数组中的相邻元素组成的。即子序列可以是不连续的。 子数组:原数组中连续的几个元素组成的数组。 1. 300【最长递增子序列】 题目: 给你一个整数数组 nums ÿ…...

Unity编辑器功能将AB资源文件生成MD5码
将路径Application.dataPath/ArtRes/AB/PC文件夹下所有的Ab包文件生成MD5吗,通过文件名 文件长度MD5‘|’的格式拼接成字符串写入到资源对比文件abCompareInfo.txt中。 将路径pathFile扥文件生成MD5码...