IO流(2)
缓冲流
字节缓冲流
利用字节缓冲区拷贝文件,一次读取一个字节:
public class test {public static void main(String [] args) throws IOException {//利用字节缓冲区来拷贝文件BufferedInputStream bis=new BufferedInputStream(new FileInputStream("a.txt"));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c.txt"));//一次读取一个字节int b;while((b=bis.read())!=-1) {bos.write(b);}bos.close();bis.close();}
}
一次读取多个字节:
public class test {public static void main(String [] args) throws IOException {//利用字节缓冲区来拷贝文件BufferedInputStream bis=new BufferedInputStream(new FileInputStream("a.txt"));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c.txt"));//一次读取多个字节int len;byte[] bytes=new byte[1024];while((len=bis.read(bytes))!=-1) {bos.write(bytes,0,len);}bos.close();bis.close();}
}
字符缓冲流
底层自带了长度为8192缓冲区提高性能。
字符缓冲输入流
特有方法:br.readLine()——读取一行数据
public class test {public static void main(String [] args) throws IOException {//字符缓冲输入流读取文件BufferedReader br=new BufferedReader(new FileReader("a.txt"));String len;while((len=br.readLine())!=null) {System.out.println(len);}//释放资源br.close();}
}
字符缓冲输出流
特有方法:bw.newLine——换行
public class test {public static void main(String [] args) throws IOException {//利用字符缓冲输出流BufferedWriter bw=new BufferedWriter(new FileWriter("c.txt"));bw.write("1,2,3");bw.newLine();//换行bw.write("4,5,6");//释放资源bw.close();}
}
综合练习
练习1:修改文本顺序
将《出师表》这个文章顺序恢复到新文件中。
分析:两种方法,一种使用ArrayList集合,一种使用TreeMap(Tree中有默认的排序规则)
第一种方法:先读取,再排序,再写入
public class test {public static void main(String [] args) throws IOException {//读取BufferedReader br=new BufferedReader(new FileReader("a.txt"));String len;ArrayList<String> list=new ArrayList<>();//将读取到的数据放入集合中while((len=br.readLine())!=null) {list.add(len);}br.close();//排序list.sort(new Comparator<String>() {//指定排序规则@Overridepublic int compare(String o1, String o2) {int i1=Integer.parseInt(o1.split("\\.")[0]); int i2=Integer.parseInt(o2.split("\\.")[0]); return i1-i2;}});//写入//需要换行写入BufferedWriter bw=new BufferedWriter(new FileWriter("c.txt"));for(String s:list) {bw.write(s);bw.newLine();}bw.close();}
}
第二种方法:利用TreeMap集合,键为序号,值为后面的字符串
public class test {public static void main(String [] args) throws IOException {//读取BufferedReader br=new BufferedReader(new FileReader("a.txt"));String len;TreeMap<Integer, String> tm=new TreeMap<>();while((len=br.readLine())!=null) {//将键值放入tm中int i=Integer.parseInt(len.split("\\.")[0]);String j=len.split("\\.")[1];tm.put(i, j);}br.close();//读取BufferedWriter bw=new BufferedWriter(new FileWriter("c.txt"));//获取键值对,利用循环Set<Map.Entry<Integer, String>> entries=tm.entrySet();for(Map.Entry<Integer, String> entry:entries) {String value=entry.getValue();bw.write(value);bw.newLine();}bw.close();}
}
练习2:软件运行次数
实现一个验证程序运行次数的小程序,要求如下:
当程序运行超过3次时给出提示:本软件只能免费使用3次,欢迎您注册会员后继续使用~2.程序运行演示如下:
第一次运行控制台输出:欢迎使用本软件第1次使用免费~
第二次运行控制台输出:欢迎使用本软件第2次使用免费~
第三次运行控制台输出:欢迎使用本软件,第3次使用免费~
第四次及之后运行控制台输出:本软件只能免费使用3次,欢迎您注册会员后继续使用~
分析:
这是一个计数器问题,利用count++完成,但如果将count写入到程序中,运行控制台重新运行后,count将恢复原始数据0,所以需要将count写入到文件中,利用读写完成。
public class test {public static void main(String [] args) throws IOException {BufferedReader br=new BufferedReader(new FileReader("c.txt"));String c=br.readLine();//读取文件中count的值int count=Integer.parseInt(c);count++;if(count<=3) {System.out.println("欢迎使用本软件第"+count+"次使用免费~");}else {System.out.println("本软件只能免费使用3次,欢迎您注册会员后继续使用~");}//将读取的count++值再写入文件中BufferedWriter bw=new BufferedWriter(new FileWriter("c.txt"));bw.write(count+"");//加入一个“”是将count变为字符串bw.close();}
}
转换流
作用:字节流想要使用字符流中的方法。
利用转换流按照指定字符编码读取,只做了解
练习::手动创建一个GBK的文件,把文件中的中文读取到内存中,不能出现乱码需求
方法1:转换流,只做了解
public class test {public static void main(String [] args) throws IOException {//利用转换流进行编码转换InputStreamReader isr=new InputStreamReader(new FileInputStream("c.txt"),"GBK");//指定读码的字符集int ch;while((ch=isr.read())!=-1) {System.out.print((char)ch);}isr.close();}
}
方法2:利用字符流按照指定编码读取(JDK11)
public class test {public static void main(String [] args) throws IOException {//字符流按照指定字符编码读取FileReader fr =new FileReader("c.txt",Charset.forName("gbk"));int ch;while((ch=fr.read())!=-1) {System.out.println((char)ch);}fr.close();}
}
练习:把一段中文按照GBK的方式写到本地文件。
方法1:转换流
public class test {public static void main(String [] args) throws IOException {//利用转换流OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("c.txt"),"GBK");osw.write("你好");osw.close();}
}
方法2:字符流
public class test {public static void main(String [] args) throws IOException {//利用转换流FileWriter fw=new FileWriter("c.txt",Charset.forName("GBK"));fw.write("你好");fw.close();}
}
练习3:利用字节流读取文件中的数据,每次读一整行,而且不能出现乱码
public class test {public static void main(String [] args) throws IOException {//利用字节流读取文件中的数据,每次读一整行,而且不能出现乱码//先是字节流,再是转换流,再是缓冲字符流BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("c.txt")));String str;while((str=br.readLine())!=null) {System.out.println(str);}br.close();}
}
序列化流
package test02;
import java.io.Serializable;
public class Student implements Serializable{private String name;private int age;public Student() {}public Student(String name,int age) {this.setName(name);this.setAge(age);}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
public class test {public static void main(String [] args) throws IOException {//序列化流:将一个java对象写入到文件中//先创建对象Student s=new Student("张三",23);//再创建序列流ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("c.txt"));os.writeObject(s);os.close();}
}
反序列化流
public class test {public static void main(String [] args) throws IOException, ClassNotFoundException {//反序列化流ObjectInputStream oi=new ObjectInputStream(new FileInputStream("c.txt"));//读取Student o=(Student)oi.readObject();System.out.println(o);oi.close();}
}
练习:用对象流读写多个对象
将多个自定义对象序列化到文件中,但是由于对象的个数不确定,反序列化流该如何读取呢?
分析:由于对象的不确定性,所以将对象放入Arraylist集合中,在读取的时候调用集合。
package test02;
import java.io.Serializable;
public class Student implements Serializable{private String name;private int age;public Student() {}public Student(String name,int age) {this.setName(name);this.setAge(age);}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
先将list集合中的内容写入文件
public class test {public static void main(String [] args) throws IOException, ClassNotFoundException {//序列化Student s1=new Student("zhangsan",23);Student s2=new Student("lisi",24);Student s3=new Student("wangwu",25);//利用集合将对象存放在集合中ArrayList<Student> list=new ArrayList<>();list.add(s1);list.add(s2);list.add(s3);//创建序列化对象ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("c.txt"));os.writeObject(list);os.close();}
}
package test02;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;public class read {public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {//读取ObjectInputStream oi=new ObjectInputStream(new FileInputStream("c.txt"));ArrayList<Student> s=(ArrayList<Student>)oi.readObject();for(Student student:s) {System.out.println(student);}}}
相关文章:

IO流(2)
缓冲流 字节缓冲流 利用字节缓冲区拷贝文件,一次读取一个字节: public class test {public static void main(String [] args) throws IOException {//利用字节缓冲区来拷贝文件BufferedInputStream bisnew BufferedInputStream(new FileInputStream(&…...
【docker】docker启动bitnami/mysql
说明:-v 宿主机目录:docker容器目录,-p 同理 注意:/opt/bitnami/mysql/conf/bitnami 目录自定义conf的目录,不能使用原有的/opt/bitnami/mysql/conf 目录。 容器启动后可在宿主机的/宿主/mysql8.0/conf,添加my_custom.…...
边缘计算、云计算、雾计算在物联网中的作用
边缘计算和雾计算不像云那样广为人知,但可以为企业和物联网公司提供很多帮助。这些网络解决了物联网云计算服务无法解决的许多问题,并使分散的数据存储适应特定的需求。让我们分别研究一下边缘计算、雾计算和云计算的优势。 雾计算的好处 低延迟。雾网络…...
【c语言】探索内存函数
探索内存函数 memcpy函数memmove函数memset函数memcmp函数: memcpy函数 memcpy函数声明: void * memcpy ( void * destination, const void * source, size_t num );将source空间下的num个字符复制到dest中去 函数的使用: 将字符数组a的5字…...

day46 完全背包理论基础 518. 零钱兑换 II 377. 组合总和 Ⅳ
完全背包理论基础 有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品都有无限个(也就是可以放入背包多次),求解将哪些物品装入背包里物品价值总和最大。 01背包内嵌的循环是从…...

【linux】运维-基础知识-认知hahoop周边
1. HDFS HDFS(Hadoop Distributed File System)–Hadoop分布式文件存储系统 源自于Google的GFS论文,HDFS是GFS的克隆版 HDFS是Hadoop中数据存储和管理的基础 他是一个高容错的系统,能够自动解决硬件故障,eg:…...

Python自动实时查询预约网站的剩余名额并在有余额时发邮件提示
本文介绍基于Python语言,自动、定时监测某体检预约网站中指定日期的体检余额,并在有体检余额时自动给自己发送邮件提醒的方法。 来到春招末期,很多单位进入了体检流程。其中,银行(尤其是四大行)喜欢“海检”…...

Flutter 验证码输入框
前言: 验证码输入框很常见:处理不好 bug也会比较多 想实现方法很多,这里列举一种完美方式,完美兼容 软键盘粘贴方式 效果如下: 之前使用 uniapp 的方式实现过一次 两种方式(原理相同)࿱…...
如何从0到设计一个CRM系统
什么是CRM 设计开始之前,先来了解一下什么是CRM。CRM(Customer Relationship Management)是指通过建立和维护与客户的良好关系,达到满足客户需求、提高客户满意度、增加业务收入的一种管理方法和策略。CRM涉及到跟踪和管理客户的所…...

Numba 的 CUDA 示例 (2/4):穿针引线
本教程为 Numba CUDA 示例 第 2 部分。 按照本系列从头开始使用 Python 学习 CUDA 编程 介绍 在本系列的第一部分中,我们讨论了如何使用 GPU 运行高度并行算法。高度并行任务是指任务完全相互独立的任务,例如对两个数组求和或应用任何元素函数。 在本教…...
项目的各个阶段如何编写标准的Git commit消息
标准提交消息格式 一个标准的提交消息应包括三部分:标题(summary)、正文(description)和脚注(footer)。 1. 标题(Summary) 简洁明了,不超过50个字符。使用…...

Python课设-学生信息管理系统
一、效果展示图 二、前端代码 1、HTML代码 <1>index.html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0">…...

openssl 常用命令demo
RSA Private Key的结构(ASN.1) RSAPrivateKey :: SEQUENCE { version Version, modulus INTEGER, -- n publicExponent INTEGER, -- e privateExponent INTEGER, -- d prime1 INTEGER, -- …...

【Linux】Linux基本指令2
目录 1.man指令(重要): 2.echo指令 3.cp指令(重要): 4.mv指令 5.cat指令/echo指令重定向 6.more指令 7.less指令(重要) 8.head指令 9.tail指令 我们接着上一篇:h…...

springboot+vue+mybatis博物馆售票系统+PPT+论文+讲解+售后
如今社会上各行各业,都喜欢用自己行业的专属软件工作,互联网发展到这个时候,人们已经发现离不开了互联网。新技术的产生,往往能解决一些老技术的弊端问题。因为传统博物馆售票系统信息管理难度大,容错率低,…...

java—MyBatis框架
简介 什么是 MyBatis? MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO&…...

如何使用Spring Cache优化后端接口?
Spring Cache是Spring框架提供的一种缓存抽象,它可以很方便地集成到应用程序中,用于提高接口的性能和响应速度。使用Spring Cache可以避免重复执行耗时的方法,并且还可以提供一个统一的缓存管理机制,简化缓存的配置和管理。 本文将详细介绍如何使用Spring Cache来优化接口,…...

大话C语言:第21篇 数组
1 数组概述 数组是若干个相同类型的变量在内存中有序存储的集合。 数组是 C 语言中的一种数据结构,用于存储一组具有相同数据类型的数据。 数组在内存中会开辟一块连续的空间 数组中的每个元素可以通过一个索引(下标)来访问,索…...

transfomer中attention为什么要除以根号d_k
简介 得到矩阵 Q, K, V之后就可以计算出 Self-Attention 的输出了,计算的公式如下: A t t e n t i o n ( Q , K , V ) S o f t m a x ( Q K T d k ) V Attention(Q,K,V)Softmax(\frac{QK^T}{\sqrt{d_k}})V Attention(Q,K,V)Softmax(dk QKT)V 好处 除以维…...

iperf3带宽压测工具使用
iperf3带宽压测工具使用 安装下载地址:[下载入口](https://iperf.fr/iperf-download.php)测试结果:时长测试(压测使用):并行测试反向测试UDP 带宽测试 iPerf3 是用于主动测试 IP 网络上最大可用带宽的工具 安装 下载地址&#x…...
React 第五十五节 Router 中 useAsyncError的使用详解
前言 useAsyncError 是 React Router v6.4 引入的一个钩子,用于处理异步操作(如数据加载)中的错误。下面我将详细解释其用途并提供代码示例。 一、useAsyncError 用途 处理异步错误:捕获在 loader 或 action 中发生的异步错误替…...
CVPR 2025 MIMO: 支持视觉指代和像素grounding 的医学视觉语言模型
CVPR 2025 | MIMO:支持视觉指代和像素对齐的医学视觉语言模型 论文信息 标题:MIMO: A medical vision language model with visual referring multimodal input and pixel grounding multimodal output作者:Yanyuan Chen, Dexuan Xu, Yu Hu…...

Spark 之 入门讲解详细版(1)
1、简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室(Algorithms, Machines, and People Lab)开发通用内存并行计算框架。Spark在2013年6月进入Apache成为孵化项目,8个月后成为Apache顶级项目,速度之快足见过人之处&…...
Spring Boot 实现流式响应(兼容 2.7.x)
在实际开发中,我们可能会遇到一些流式数据处理的场景,比如接收来自上游接口的 Server-Sent Events(SSE) 或 流式 JSON 内容,并将其原样中转给前端页面或客户端。这种情况下,传统的 RestTemplate 缓存机制会…...
条件运算符
C中的三目运算符(也称条件运算符,英文:ternary operator)是一种简洁的条件选择语句,语法如下: 条件表达式 ? 表达式1 : 表达式2• 如果“条件表达式”为true,则整个表达式的结果为“表达式1”…...

Mac软件卸载指南,简单易懂!
刚和Adobe分手,它却总在Library里给你写"回忆录"?卸载的Final Cut Pro像电子幽灵般阴魂不散?总是会有残留文件,别慌!这份Mac软件卸载指南,将用最硬核的方式教你"数字分手术"࿰…...
土地利用/土地覆盖遥感解译与基于CLUE模型未来变化情景预测;从基础到高级,涵盖ArcGIS数据处理、ENVI遥感解译与CLUE模型情景模拟等
🔍 土地利用/土地覆盖数据是生态、环境和气象等诸多领域模型的关键输入参数。通过遥感影像解译技术,可以精准获取历史或当前任何一个区域的土地利用/土地覆盖情况。这些数据不仅能够用于评估区域生态环境的变化趋势,还能有效评价重大生态工程…...

SpringTask-03.入门案例
一.入门案例 启动类: package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...
浅谈不同二分算法的查找情况
二分算法原理比较简单,但是实际的算法模板却有很多,这一切都源于二分查找问题中的复杂情况和二分算法的边界处理,以下是博主对一些二分算法查找的情况分析。 需要说明的是,以下二分算法都是基于有序序列为升序有序的情况…...

基于TurtleBot3在Gazebo地图实现机器人远程控制
1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...