4.20 IO流

IO流结构

InputStream(字节输入流)

public static void main(String[] args)  {// byteInputStream();// byteInputStream1();// byteInputStream2();byteInputStream3();}// 使用字节流时对于中文汉字基本都会出现乱码问题,因此对中文乱码问题通常用字符流Reader和Writer// 字节输入流(手动关闭资源),一个一个字节读,速度较慢public static void byteInputStream() {InputStream inputStream = null;try {inputStream = new FileInputStream("C:\\Users\\21941\\Desktop\\sql.txt");int read = 0;while ((read = inputStream.read()) != -1) {System.out.print((char) read);}}catch (IOException e) {throw new RuntimeException(e);} finally {try {inputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}// 字节输入流(自动关闭资源),一个一个字节读,速度较慢public static void byteInputStream1() {try (InputStream inputStream = new FileInputStream("C:\\Users\\21941\\Desktop\\sql.txt");) {int read = 0;while ((read = inputStream.read()) != -1) {System.out.print((char) read);}} catch (IOException e) {throw new RuntimeException(e);}}// 字节输入流(自动关闭资源),一个一个字节读,将读取的先字节存入数组,一同取出,速度较快public static void byteInputStream2()  {try (InputStream inputStream = new FileInputStream("C:\\Users\\21941\\Desktop\\sql.txt");) {byte[] bytes = new byte[1024];while ((inputStream.read(bytes)) != -1) {System.out.print(new String(bytes));}} catch (IOException e) {throw new RuntimeException(e);}}// 字节输入流(自动关闭资源),一个一个字节读,将读取的先字节存入数组,一同取出,速度较快public static void byteInputStream3()  {try (InputStream inputStream = new FileInputStream("C:\\Users\\21941\\Desktop\\sql.txt");) {byte[] bytes = new byte[1024];int temp=0;while ((temp=inputStream.read(bytes)) != -1) {// 防止读到最后时不能全部覆盖之前的,输出每一次读取的内容System.out.print(new String(bytes,0,temp));}} catch (IOException e) {throw new RuntimeException(e);}}

OutputStream(字节输出流)

public static void OutputStream(){// 没有文件则自动新建/*OutputStream outputStream = new FileOutputStream(路径)OutputStream outputStream = new FileOutputStream(路径,是否追加(默认false))*/try (OutputStream outputStream = new FileOutputStream("D:\\soft\\javawork\\javawork\\one\\four\\src\\com\\tianliang\\io0\\outputstream.txt")){outputStream.write(97);byte[] bytes = {10,23,52,99,45,3,15};outputStream.write(bytes);// outputStream.write(字节数组/整形/字节数组,偏移量,长度);// 输出流需要刷新outputStream.flush();}catch (IOException e){e.printStackTrace();}}

Reader(字符输入流)

public static void reader(){// 用自动关闭方式try (Reader reader = new FileReader("C:\\Users\\21941\\Desktop\\sql.txt")){char[] chars = new char[1024];int temp=0;while ((temp =reader.read(chars))!=-1){System.out.print(new String(chars,0,temp));// reader.read(字符数组/字符数组,偏移量,长度);}}catch (IOException e) {throw new RuntimeException(e);}}

Writer(字符输出流)

/*FileWriter writer = new FileWriter(路径)FileWriter writer = new FileWriter(路径,是否追加(默认false))*/public static void writer() {try (Writer writer = new FileWriter("D:\\soft\\javawork\\javawork\\one\\four\\src\\com\\tianliang\\io0\\writer.txt")) {writer.write(97);String string = "你好";writer.write(string);char[] chars = {'A', 'S', 'G', 'H', 'T', 'E', 'F', 'd', 'g', 'b', 'n', 'm', '中', '国'};writer.write(chars);writer.flush();} catch (IOException e) {throw new RuntimeException(e);}}

InputStreamReader(字节输入转字符输入流)

处理字节乱码问题,也可以处理编码乱码问题

public static void main(String[] args) {// 转换流,还可以通过设置字符编码解决乱码try {FileInputStream fis = new FileInputStream("E:/test.txt");// 转换的时候,还可以指定字符编码,用于解决乱码问题// 默认UTF-8InputStreamReader isr = new InputStreamReader(fis,"GBK");InputStreamReader isr = new InputStreamReader(fis,编码);int temp = 0;while ((temp = isr.read()) != -1) {System.out.print((char)temp);}} catch (Exception e) {e.printStackTrace();}}
public static void main(String[] args) {try (// 字节输入流FileInputStream fis = new FileInputStream(路径);// 转换为字符输入流,为什么不是FileReader类型呢?// 因为FileReader 继承了InputStreamReader 所以一样InputStreamReader isr = new InputStreamReader(fis);){// 和字符输入流使用方式一样// 读取char数组// isr.read(chars);// 读取一个字符// isr.read();} catch (Exception e) {e.printStackTrace();}}

OutputStreamWriter(字节输出转字符输出流)

public static void main(String[] args) {try (// 字节输出流FileOutputStream fos = new FileOutputStream(路径);// 转换为字符输出流OutputStreamWriter osw = new OutputStreamWriter(fos);){// 和字符输出流用法一样osw.write("xxx");osw.flush();} catch (Exception e) {e.printStackTrace();}}

BufferedReader(字符输入缓冲流)

提高读取速度,增加按行读的方法readLine();剩余使用方法和字符输入流一样

public static void main(String[] args) {try (// 字符输入流,传入文件路径FileReader fr = new FileReader(路径);// 字符输入缓冲流,传入字符输入流BufferedReader br = new BufferedReader(fr);){// 使用方式和字符输入流一样// br.read();// br.read(chars)// 新增读取一行的方法,返回读取到的这一行数据// 到达文件末尾,返回null// String temp = br.readLine();String temp = null;while ((temp=br.readLine()) != null) {System.out.println(temp);}} catch (Exception e) {e.printStackTrace();}}

BufferedWriter(字符输出缓存流)

增加写出速度,增加换行写出方法newLine();其余方式使用和字符输出流一样

public static void main(String[] args) {try (// 字符输出流,传入文件路径,和 是否覆盖写入FileWriter fw = new FileWriter("./src/com/test.txt");// 字符输出缓冲流,传入字符输出流对象BufferedWriter bw = new BufferedWriter(fw);){// 用法和字符输出流一样// bw.write(chars);// bw.write(int);// bw.write("xx");// 新增写出换行的方法bw.newLine();// 刷缓存bw.flush();} catch (Exception e) {e.printStackTrace();}}

测试字符流与字符缓存流速度

public static void main(String[] args) {fileInputStreamTest(路径);bufferedFileInputStreamTest(路径);}public static void fileInputStreamTest(String path){long startTime = System.currentTimeMillis();try (FileInputStream fis =new FileInputStream(path);){int temp = 0;byte[] bytes = new byte[1024];while ((temp = fis.read(bytes)) != -1) {}} catch (Exception e) {e.printStackTrace();}long endTime = System.currentTimeMillis();System.out.println("读取完成,字节输入流耗时 : "+(endTime-startTime));}public static void bufferedFileInputStreamTest(String path){long startTime = System.currentTimeMillis();try (FileInputStream fis =new FileInputStream(path);BufferedInputStream bis = new BufferedInputStream(fis);){int temp = 0;byte[] bytes = new byte[1024];while ((temp = bis.read(bytes)) != -1) {//写入时需要执行的内容}} catch (Exception e) {e.printStackTrace();}long endTime = System.currentTimeMillis();System.out.println("读取完成,字节输入缓冲流耗时 : "+(endTime-startTime));}

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

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

相关文章

mininet+odl安装

安装环境 ubuntu-18.04.2-desktop-amd64 Java version: 1.8.0_362 Apache Maven 3.6.0 opendaylight: distribution-karaf-0.6.0-Carbon(csdn中应该是已有资源,不让上传) opendaylight的官网下载链接一直打开失败,我使用的是别人的Carbon版本。 在安…

yml文件解析

.yml 后缀的文件可以有多个application.yml # 项目相关配置 用于 RuoYiConfig.java ruoyi:# 名称name: RuoYi# 版本version: 3.8.5# 版权年份copyrightYear: 2023# 实例演示开关demoEnabled: true# 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Lin…

C语言结构体,枚举,联合

系列文章目录 第一章 C语言基础知识 第二章 C语言控制语句 第三章 C语言函数详解 第四章 C语言数组详解 第五章 C语言操作符详解 第六章 C语言指针详解 第七章 C语言结构体详解 第八章 详解数据在内存中的存储 第九章 C语言指针进阶 文章目录 1. 结构体 1.1 声明结构…

kubebuilder(2)创建项目及初始化

一个demo项目来了解kubebuilder的项目结构 初始化项目 mkdir demo-operator cd demo-operator kubebuilder init --domain demo.com --repo demo.com/tutorial 这一步创建了 Go module 工程基本的模板文件,引入了必要的依赖 如果不用--repo参数,也可…

【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Push Button的使用及说明

博客主页:Duck Bro 博客主页系列专栏:Qt 专栏关注博主,后期持续更新系列文章如果有错误感谢请大家批评指出,及时修改感谢大家点赞👍收藏⭐评论✍ Qt常用控件 | 按钮类控件 | Push Button的使用及说明 文章编号&#x…

mysql基础6——多表查询

外键 把分散在多个不同表里面的数据查询出来的操作,就是多表查询 把两个表连接:使用外键(foreign key)和连接(join) 外键在表创建的阶段定义也可以通过修改表定义,连接在查询字段把相同意义的字段连接起来 外键就是从表中用来引用主表中数…

C# 开源SDK 工业相机库 调用海康相机 大恒相机

C# MG.CamCtrl 工业相机库 介绍一、使用案例二、使用介绍1、工厂模式创建实例2、枚举设备,初始化3、启动相机4、取图5、注销相机 三、接口1、相机操作2、启动方式3、取图4、设置/获取参数 介绍 c# 相机库,含海康、大恒品牌2D相机的常用功能。 底层采用回…

ai扩写软件有哪些免费的?分享4款扩写好用的!

随着人工智能技术的飞速发展,AI扩写软件逐渐成为了内容创作者们的得力助手。它们能够迅速将简短的文案扩写成内容丰富、结构完整的文章,大大提高了创作效率。本文将为您盘点几款免费的AI扩写软件,助您在今日头条、百家号等自媒体平台上轻松打…

P8837 [传智杯 #3 决赛] 商店(贪心加双指针)

题目背景 思路解析:很经典的贪心问题,把物品按照从便宜到贵的顺序排好序,然后按照富贵程度排人,直接暴力会tle所以这里采用双指针. #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<string> using namesp…

quill富文本编辑器中文汉化和高度设置操作

quill文档&#xff1a;Installation - Quill Rich Text Editor quill仓库&#xff1a;GitHub - quilljs/quill: Quill is a modern WYSIWYG editor built for compatibility and extensibility. quill默认是英文的&#xff0c;并且高度也是只有一行&#xff0c;所以想自定义高…

C++——类和对象练习(日期类)

日期类 1. 构造函数和析构函数2. 拷贝构造和赋值运算符重载3. 运算符重载3.1 日期的比较3.2 日期加减天数3.3 日期减日期3.4 流插入和流提取 4. 取地址和const取地址重载5. 完整代码Date.hDate.c 对日期类进行一个完善&#xff0c;可以帮助我们理解六个默认成员函数&#xff0c…

软件行业中的蓝海领域有哪些?

一、什么是蓝海&#xff1f; 蓝海&#xff0c;指的是未知的市场空间。这个概念相对于“红海”而言&#xff0c;红海则是指已知的市场空间。 企业要启动和保持获利性增长&#xff0c;就必须超越产业竞争&#xff0c;开创全新市场&#xff0c;这其中包括两块&#xff1a;一块是…

【软件测试】Selenium实战技巧-多页面和Windows控件处理

01 多页面处理 做UI自动化的时候常常会遇到浏览器弹出新的Tab页&#xff0c;或者需要在多个网页服务之间来回取数据的情况。 比如在首页点击文章“Jmeter使用&#xff1f;”的链接&#xff0c;浏览器会弹出一个新的页面显示“Jmeter使用&#xff1f;”这篇文章的详情。此时如…

Python多线程与多进程编程

一、引言 随着计算机技术的飞速发展&#xff0c;程序运行的速度和效率成为了人们关注的焦点。为了提高程序的执行效率&#xff0c;多线程与多进程编程技术应运而生。Python作为一种通用编程语言&#xff0c;在支持多线程与多进程编程方面有着独特的优势。本文将详细探讨Python…

书生·浦语大模型实战营之 XTuner 微调 Llama 3 个人小助手认知

书生浦语大模型实战营之 XTuner 微调 Llama 3 个人小助手认知 Llama 3 近期重磅发布,发布了 8B 和 70B 参数量的模型,XTuner 团队对 Llama 3 微调进行了光速支持!!!开源同时社区中涌现了 Llama3-XTuner-CN 手把手教大家使用 XTuner 微调 Llama 3 模型。 XTuner:http://…

牛客NC238 加起来和为目标值的组合【中等 DFS C++、Java、Go、PHP】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/172e6420abf84c11840ed6b36a48f8cd 思路 本题是组合问题&#xff0c;相同元素不同排列仍然看作一个结果。 穷经所有的可能子集&#xff0c;若和等于target&#xff0c;加入最终结果集合。 给nums排序是为了方便…

计算机网络【CN】Ch4 网络层

总结 一台主机可以有多个IP地址&#xff0c;但是必须属于多个逻辑网络【不同的网络号】。 解决IP地址耗尽&#xff1a; IP地址结构&#xff1a; 划分子网&#xff1a;&#x1d43c;&#x1d443;地址<网络号>,<子网号>,<主机号> CIDR&#xff1a;IP地址{&…

智能算法 | Matlab基于CBES融合自适应惯性权重和柯西变异的秃鹰搜索算法

智能算法 | Matlab基于CBES融合自适应惯性权重和柯西变异的秃鹰搜索算法 目录 智能算法 | Matlab基于CBES融合自适应惯性权重和柯西变异的秃鹰搜索算法效果一览基本介绍程序设计参考资料效果一览 基本介绍 Matlab基于CBES融合自适应惯性权重和柯西变异的秃鹰搜索算法 融合自适应…

pycharm爬虫模块(scrapy)基础使用

今天学了个爬虫。在此记录 目录 一.通过scrapy在命令行创建爬虫项目 二.判断数据为静态还是动态 三.pycharm中的设置 三:爬虫主体 四.pipelines配置&#xff08;保存数据的&#xff09; 五.最终结果 一.通过scrapy在命令行创建爬虫项目 1.首先需要在cmd中进入到python文…

华为od机试真题——智能成绩表

题目描述 算法结果 3 3 math chinese english z3 71 81 91 l4 81 71 93 w5 21 91 95 math l4 81 71 93 245 z3 71 81 91 243 w5 21 91 95 207 算法详情 public class SmartScoreTable {public static void main(String[] args) {// 输入Scanner scanner new Scanner(Syste…