邵阳做网站价格/百度下载app
文章目录
- 一、文件读写操作FileReader和FileWriter
- 1.main()
- 2.FileReader
- 1.说明:
- 2.代码案例
- 3.对read()操作升级:使用read的重载方法
- 4.FileWriter的使用
- 1.说明
- 2.代码
- 4.FileReader和FileWriter综合使用
- 二、使用步骤
- 1.引入库
- 二、测试FileInputStream和FileOutputStream的使用
- 1.结论
- 2.使用字节流FileInputStream处理文本文件,可能出现乱码
- 3.实现对图片的复制操作
- 3.指定路径下的文件的复制
- 三、处理流之一:缓冲流的使用
- 1.缓冲流
- 2.作用
- 3.处理流,就是“套接”在已有的流的基础上
- 4.实现非文本文件的复制BufferedInputStream和BufferedOutputStream
- 5.实现文件复制的方法BufferedInputStream和BufferedOutputStream
- 6.使用BufferedReader和BufferedWriter实现文本文件的复制
- 四、处理流之二:转换流的使用
- 1.概念:
- 2.作用
- 3.InputStreamReader
- 4.InputStreamReader和OutputStreamWriter
- 五、流的分类以及流的体系结构
- 六、输入、输出的标准化过程
- 1.输入
- 2.输出
提示:以下是本篇文章正文内容,下面案例可供参考
一、文件读写操作FileReader和FileWriter
1.main()
public static void main(String[] args) {File file = new File("hello.txt"); //相较于当前工程System.out.println(file.getAbsolutePath());File file1 = new File("day04\\hello.txt");System.out.println(file.getAbsolutePath());}
2.FileReader
1.说明:
1.将hello.txt文件内容读入到程序中,并输出到控制台4
2…read()的处理,返回读入的一个字符,如果达到文件末尾,返回-1
3.异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
4.读入的文件一定要存在,否则就会报FileNotFoundException.
2.代码案例
public void testFileReader() {FileReader fr = null;try{//1.实例化File类的对象,指明要操作的文件File file = new File("hello.txt"); //相较于当前Module//System.out.println(file.getAbsolutePath());//2.提供具体的流fr = new FileReader(file);//3.数据的读入//read():返回读入的一个字符。如果到达文件末尾,返回-1.
// 方式一:
// int data = fr.read();
// while(data != -1){
// System.out.print((char)data);
// data = fr.read();
// }//方式二: 语法上针对于方式一的修改int data;while((data = fr.read())!=-1){System.out.println((char)data);}}catch(IOException e){e.printStackTrace();}finally {//4.流的关闭操作try{if(fr!=null)fr.close();}catch(IOException e){e.printStackTrace();}}}
3.对read()操作升级:使用read的重载方法
public void testFileReader1() {FileReader fr = null;try{//1.File类的实例化File file = new File("hello.txt");//2.FileReader流的实例化fr = new FileReader(file);//3.读入的操作//read(char[] cbuf) :返回每次读入到cbuf数组中的字符的个数。//如果达到文件末尾,返回-1。char[] cbuf = new char[5];int len;while((len = fr.read(cbuf))!=-1){//错误的写法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }//方式一:
// for (int i = 0; i < len; i++) {
// System.out.print(cbuf[i]);
//
// }//方式二:错误!
// String str = new String(cbuf);
// System.out.println(str);//正确String str = new String(cbuf,0,len);System.out.print(str);}}catch(IOException e){e.printStackTrace();}finally {//4.流的关闭操作try{if(fr!=null)fr.close();}catch(IOException e){e.printStackTrace();}}}
4.FileWriter的使用
1.说明
1.从内存中写出数据到硬盘的文件里。
2.输出操作,对应的File可以不存在的,并不会报异常
3.File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件
4.File对应的硬盘中的文件如果存在,
//如果流使用的构造器是: FileWriter(file,false)/(FileWriter(file)):对原有文件的覆盖
//如果流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是追加。
2.代码
public void testFileWriter() {FileWriter fw = null;try{//1.提供File类的对象,指明写出到的文件File file = new File("hello1.txt");//2.提供FileWriter的对象,用于数据的写出fw = new FileWriter(file,true);//3.写出的操作fw.write("I have a dream!\n");fw.write("you need to have a dream!");}catch(IOException e){e.printStackTrace();}finally {//4.流的关闭操作try{if(fw!=null)fw.close();}catch(IOException e){e.printStackTrace();}}}
4.FileReader和FileWriter综合使用
public void testFileReaderFileWriter(){FileReader fr = null;FileWriter fw = null;try{//1.创建File类的对象,指明读入和写出的文件File srcFile = new File("hello.txt");File destFile = new File("hello2.txt");//不能使用字符流来处理图片等字节数据
// File srcFile = new File("111.jpg");
// File destFile = new File("2.jpg");//2.创建输入流和输出流的对象fr = new FileReader(srcFile);fw = new FileWriter(destFile);//3.数据的读入和写出操作char[] cbuf = new char[5];int len;//记录每次读入到cbuf数组中的字符的个数while((len = fr.read(cbuf))!= -1){//每次写出len个字符fw.write(cbuf,0,len);}}catch (IOException e){e.printStackTrace();}finally {//4.关闭数据流try{if(fw!=null)fw.close();}catch (IOException e){e.printStackTrace();}finally {try{if(fr!=null)fr.close();}catch(IOException e){e.printStackTrace();}}}}
二、使用步骤
1.引入库
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
二、测试FileInputStream和FileOutputStream的使用
1.结论
1.对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
2.对于非文本文件(.jpg,.mp3,.mp4,.avi.doc,.ppt,…),使用字节流处理
2.使用字节流FileInputStream处理文本文件,可能出现乱码
public void testFileInputStream(){FileInputStream fis = null;try{//1.造文件File file = new File("hello.txt");//2.造流fis = new FileInputStream(file);//3.读数据byte[] buffer = new byte[5];int len; //记录每次读取的字节的个数while((len = fis.read(buffer)) != -1){String str = new String(buffer,0,len);System.out.println(str);}}catch(IOException e){e.printStackTrace();}finally {try{if(fis != null){fis.close();}}catch(IOException e){e.printStackTrace();}}}
3.实现对图片的复制操作
public void testFileInputOutputStream(){FileInputStream fis = null;FileOutputStream fos = null;try{File srcFile = new File("111.jpg");File destFile = new File("2.jpg");fis = new FileInputStream(srcFile);fos = new FileOutputStream(destFile);//复制的过程byte[] buffer = new byte[5];int len;while((len = fis.read(buffer))!= -1){fos.write(buffer,0,len);}}catch (IOException e){e.printStackTrace();}finally {if(fos != null){try{fos.close();}catch(IOException e){e.printStackTrace();}}}}
3.指定路径下的文件的复制
public void copyFile(String srcPath,String destPath){FileInputStream fis = null;FileOutputStream fos = null;try{File srcFile = new File(srcPath);File destFile = new File(destPath);fis = new FileInputStream(srcFile);fos = new FileOutputStream(destFile);//复制的过程byte[] buffer = new byte[1024];int len;while((len = fis.read(buffer))!= -1){fos.write(buffer,0,len);}}catch (IOException e){e.printStackTrace();}finally {if(fos != null){try{fos.close();}catch(IOException e){e.printStackTrace();}}}}@Testpublic void testCopyFile(){long start = System.currentTimeMillis();String srcPath = "C:\\Users\\asus\\Desktop\\1.mp4";String destPath = "C:\\Users\\asus\\Desktop\\2.mp4";copyFile(srcPath,destPath);long end = System.currentTimeMillis();System.out.println("复制操作花费的时间为:"+(end-start));}
三、处理流之一:缓冲流的使用
1.缓冲流
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
2.作用
提供流的读取、写入的速度
提高读写速度的原因,内部提供了一个缓冲区
3.处理流,就是“套接”在已有的流的基础上
4.实现非文本文件的复制BufferedInputStream和BufferedOutputStream
public void BufferedStreamTest(){BufferedInputStream bis = null;BufferedOutputStream bos = null;try{//1.造文件File srcFile = new File("111.jpg");File destFile = new File("222.jpg");//2.造流//2.1 造节点流FileInputStream fis = new FileInputStream(srcFile);FileOutputStream fos = new FileOutputStream(destFile);//2.2造缓冲流bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);//3.复制的细节:读取、写入byte[] buffer = new byte[10];int len;while((len = bis.read(buffer))!=-1){bos.write(buffer,0,len);}}catch (IOException e){e.printStackTrace();}finally {//4.资源关闭//要求:先关闭外层的流,再关闭内层的流//说明:关闭外层流的同时,内层流也会自动的进行关闭。//关于内层流的关闭,我们可以省略if(bos!= null){try{bos.close();}catch (IOException e){e.printStackTrace();}}if(bis!=null){try{bis.close();}catch(IOException e){e.printStackTrace();}}}}
5.实现文件复制的方法BufferedInputStream和BufferedOutputStream
public void copyFileWithBufferd(String srcPath,String destPath){BufferedInputStream bis = null;BufferedOutputStream bos = null;try{//1.造文件File srcFile = new File(srcPath);File destFile = new File(destPath);//2.造流//2.1 造节点流FileInputStream fis = new FileInputStream(srcFile);FileOutputStream fos = new FileOutputStream(destFile);//2.2造缓冲流bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);//3.复制的细节:读取、写入byte[] buffer = new byte[1024];int len;while((len = bis.read(buffer))!=-1){bos.write(buffer,0,len);// bos.flush();//刷新缓冲流}}catch (IOException e){e.printStackTrace();}finally {//4.资源关闭//要求:先关闭外层的流,再关闭内层的流//说明:关闭外层流的同时,内层流也会自动的进行关闭。//关于内层流的关闭,我们可以省略if(bos!= null){try{bos.close();}catch (IOException e){e.printStackTrace();}}if(bis!=null){try{bis.close();}catch(IOException e){e.printStackTrace();}}}}
public void testCopyFileWithBuffered(){long start = System.currentTimeMillis();String srcPath = "C:\\Users\\asus\\Desktop\\1.mp4";String destPath = "C:\\Users\\asus\\Desktop\\3.mp4";copyFileWithBufferd(srcPath,destPath);long end = System.currentTimeMillis();System.out.println("复制操作花费的时间为:"+(end-start));}
6.使用BufferedReader和BufferedWriter实现文本文件的复制
public void testBufferedReaderBufferedWriter(){BufferedReader br = null;BufferedWriter bw = null;try{//1.创建文件和相应的流br = new BufferedReader(new FileReader(new File("hello.txt")));bw = new BufferedWriter(new FileWriter(new File("hello3.txt")));//2.读写操作//方式一:
// char[] cbuf = new char[1024];
// int len;
// while((len = br.read(cbuf))!=-1){
// bw.write(cbuf,0,len);
bw.flush();
// }//方式二:String data;while((data = br.readLine())!=null){//方法一:
// bw.write(data+"\n"); //data不包含换行符//方法二:bw.write(data);bw.newLine(); //提供换行的操作}}catch(IOException e){e.printStackTrace();}finally {//3.关闭资源try{if(bw!=null)bw.close();}catch (IOException e){e.printStackTrace();}try{if(br!=null)br.close();}catch (IOException e){e.printStackTrace();}}}
四、处理流之二:转换流的使用
1.概念:
转换流:属于字符流
InputstreamReader: 将一个字节的输入流转换为字符的输入流Outputstreamwriter: 将一个字符的输出流转换为字节的输出流
2.作用
提供字节流与字符流之间的转换
3.InputStreamReader
//此时处理异常的话,仍然应该使用try-catch-finally//InputStreamReader的使用,实现字节的输入流到字符的输入流的转换@Testpublic void test1() throws IOException {FileInputStream fis = new FileInputStream("hello.txt");
// InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集//参数2指明了字符集,具体使用哪个字符集,取决于文件hello.txt保存时使用的字符集InputStreamReader isr = new InputStreamReader(fis,"UTF-8");char [] cbuf = new char[20];int len;while((len = isr.read(cbuf))!=-1){String str = new String(cbuf,0,len);System.out.print(str);}isr.close();}
4.InputStreamReader和OutputStreamWriter
//此时处理异常的话,仍然应该使用try-catch-finally//综合使用InputStreamReader和OutputStreamWriter@Testpublic void test2() throws IOException {//1.造文件 造流File file1 = new File("hello.txt");File file2 = new File("hello_gbk.txt");FileInputStream fis = new FileInputStream(file1);FileOutputStream fos = new FileOutputStream(file2);InputStreamReader isr = new InputStreamReader(fis,"utf-8");OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");//读写过程char [] cbuf = new char[20];int len;while((len = isr.read(cbuf))!=-1){osw.write(cbuf,0,len);}//3.关闭资源isr.close();osw.close();}
五、流的分类以及流的体系结构
六、输入、输出的标准化过程
1.输入
2.输出
相关文章:

Java流的体系结构(一)
文章目录 一、文件读写操作FileReader和FileWriter1.main()2.FileReader1.说明:2.代码案例 3.对read()操作升级:使用read的重载方法4.FileWriter的使用1.说明2.代码 4.FileReader和FileWriter综合使用 二、使用步骤1.引入库 二、测试FileInputStream和Fi…...

什么是Redux?它的核心概念有哪些?
聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 什么是Redux?⭐ 它的核心概念有哪些?⭐ 写在最后 ⭐ 专栏简介 前端入门之旅:探索Web开发的奇妙世界 欢迎来到前端入门之旅!感兴趣的可以订阅本专栏哦!这个专栏是为那些对Web开发…...

细胞机器人系统中的群体智能
摘要 细胞机器人系统具有“智能”行为能力。本文分析了这种智能的含义。本文根据上述不可思议智能行为的不可预测性来定义机器人智能和机器人系统智能。对不可预测性概念的分析与(1)统计不可预测、(2)不可访问、(3&am…...

【办公自动化】用Python将PDF文件转存为图片(文末送书)
🤵♂️ 个人主页:艾派森的个人主页 ✍🏻作者简介:Python学习者 🐋 希望大家多多支持,我们一起进步!😄 如果文章对你有帮助的话, 欢迎评论 💬点赞Ǵ…...

不容易解的题9.26
想编写这一版,是因为之前复习字符串或者双指针等其他栏目时候没有写文章,但是现在回过头来刷,所以想着写一篇,我在leetcode的收藏夹里收藏了一些我自认为需要多加练习的题目,它们并非是很难的,极不易理解的…...

易点易动固定资产管理系统:精准管理与科学采购,降本增效的利器
在现代企业管理中,固定资产的精准管理和科学采购已成为提升企业效率和降低成本的重要环节。为了满足企业管理的需求,我们自豪地介绍易点易动固定资产管理系统,这是一款功能强大的软件解决方案,旨在帮助企业实现固定资产的精准管理…...

人大金仓分析型数据库外部表(二)
外部表错误数据 默认情况下,如果外部表数据中包含有一个错误,命令就会失败并且不会有数据被载入到目标数据库表中。gpfdist 文件服务器使用 HTTP 协议。使用 LIMIT的外部表查询会在检索到所需的 行后结束连接,导致一个HTTP 套接字错误。 如…...

rtp流广播吸顶喇叭网络有源吸顶喇叭
SIP-7043 rtp流广播吸顶喇叭网络有源吸顶喇叭 一、描述 SIP-7043是我司的一款SIP网络有源吸顶喇叭,具有10/100M以太网接口,内置有一个高品质扬声器,将网络音源通过自带的功放和喇叭输出播放,可达到功率20W。SIP-7043作为SIP系统的…...

Spring学习笔记12 面向切面编程AOP
Spring学习笔记11 GoF代理模式_biubiubiu0706的博客-CSDN博客 AOP(Aspect Oriented Programming):面向切面编程,面向方面编程. AOP是对OOP的补充延申.底层使用动态代理实现. Spring的AOP使用的动态代理是:JDK动态代理_CGLIB动态代理技术.Spring在这两种动态代理中灵活切换.如…...

【0225】源码分析postgres磁盘块(disk block)定义
相关阅读: 【0040】 PostgreSQL数据库表文件底层结构布局分析 1. postgres磁盘块定义 在学习本文之前,需要对关系表的结构原理有一定的理解。如果不清楚PG磁盘数据表文件的布局,可阅读:...

第九章 动态规划 part11 123. 买卖股票的最佳时机III 188. 买卖股票的最佳时机IV
第五十天| 第九章 动态规划 part11 123. 买卖股票的最佳时机III 188. 买卖股票的最佳时机IV 一、123. 买卖股票的最佳时机III(难难难难难) 题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/ 题目介绍ÿ…...

阿里云服务器共享型和企业级独享有什么区别?
阿里云ECS云服务器共享型和企业级有什么区别?企业级就是独享型,共享型和企业级云的主要区别CPU调度模式,共享型是非绑定CPU调度模式,企业级是固定CPU调度模式,共享型云服务器在高负载时计算性能可能出现波动不稳定&…...

Vue.js基本语法上
🎬 艳艳耶✌️:个人主页 🔥 个人专栏 :《Spring与Mybatis集成整合》《springMvc使用》 ⛺️ 生活的理想,为了不断更新自己 ! 目录 1.插值 1.1 文本 1.2 v-v-html 1.3 数据双向绑定数据(v-model) 1.4 属性ÿ…...

【1333. 餐厅过滤器】
来源:力扣(LeetCode) 描述: 给你一个餐馆信息数组 restaurants,其中 restaurants[i] [idi, ratingi, veganFriendlyi, pricei, distancei]。你必须使用以下三个过滤器来过滤这些餐馆信息。 其中素食者友好过滤器 v…...

wifi7有关的210个提案
[1] TGbe, “Compendium of motions related to the contents of the TGbe specification framework document,” 19/1755r8, September 2020. [2] Bin Tian (Qualcomm), “Discussion on 11be PHY capabilities,” 20/0975r0, July 2020. [3] TGbe, “Compendiu…...

200行C++代码写一个Qt俄罗斯方块小游戏
小小演示一下: 大体思路: 其实很早就想写一个俄罗斯方块了,但是一想到那么多方块还要变形,还要判断落地什么的就脑壳疼。直到现在才写出来。 俄罗斯方块这个小游戏的小难点其实就一个,就是方块的变形,看似…...

蓝桥杯每日一题20223.9.26
4407. 扫雷 - AcWing题库 题目描述 分析 此题目使用map等都会超时,所以我们可以巧妙的使用哈希模拟散列表,哈希表初始化为-1首先将地雷读入哈希表,找到地雷的坐标在哈希表中对应的下标,如果没有则此地雷的位置第一次出现&#…...

查看基站后台信息
查看基站后台信息 电脑配置固定ip: 192.168.1.99: 打开“网络和共享中心”,选择更改适配器设置: 右键“本地连接”,选择属性 基站网线直连电脑网口 Telnet 登录基站 打开dos窗口 windows键R”,输入cmd,点确定&…...

关于坐标的旋转变换和坐标系的旋转变换
不管是坐标的旋转变换还是坐标系下的旋转变换,只和旋转的顺时针和逆时针有关。然坐标系间的顺时针和逆时针是根据当前坐标系在目标坐标系下的相对位置确定。 一。逆时针旋转belta角度的公式 二。顺时针旋转belta角度的公式 三。坐标的旋转变换 1.坐标的旋转变换相…...

2023.9.19 关于 数据链路层 和 DNS 协议 基本知识
目录 数据链路层 MTU DNS 协议 补充 DHCP协议 数据链路层 基本概念: 考虑相邻两个节点之间的传输(通过 网线 / 光纤 / 无线 直接相连的两个设备)以太网协议 规定了 数据链路层 和 物理层 的内容 IP地址 与 mac地址 的相互配合 IP地址 描…...

如何保证接口幂等性
简介 接口幂等性就是说用户使用相同的参数请求同一个接口无论是一次还是多次都应该是一样的。不会因为多次的点击产生不同效果。 举个栗子:一个用户在手机APP上提200块钱,然后一不小心点击了两次,那么就应该只提取出200块钱,不应…...

搭建智能桥梁,Amazon CodeWhisperer助您轻松编程
零:前言 随着时间的推移,人工智能技术以惊人的速度向前发展,正掀起着全新的编程范式革命。不仅仅局限于代码生成,智能编程助手等创新应用也进一步提升了开发效率和代码质量,极大地推动着软件开发领域的快速繁荣。 当前…...

数组和指针笔试题解析之【指针】
目录 🍂笔试题1: 🍂笔试题2: 🍂笔试题3: 🍂笔试题4: 🍂笔试题5: 🍂笔试题6: 🍂笔试题7: 🍂笔试题…...

【Linux】之Centos7卸载KVM虚拟化服务
👨🎓博主简介 🏅云计算领域优质创作者 🏅华为云开发者社区专家博主 🏅阿里云开发者社区专家博主 💊交流社区:运维交流社区 欢迎大家的加入! 🐋 希望大家多多支…...

智能电力运维系统:数字化转型在电力行业的关键应用
随着信息技术、人工智能等的飞速发展,数字化改造已成为各行各业的重要发展趋势。在电力行业中,智能电力运维系统是数字化转型的关键应用之一。 力安科技智能电力运维系统是一种集自动化、智能化、云计算、物联网等先进技术于一体的电力运维管理解决方…...

eslint报错:no-empty-source
问题: 提交代码时,eslint校验没有通过,报错 esc[2m183:9esc[22m esc[31mesc[31m✖esc[39m Unexpected empty source esc[2mno-empty-sourceesc[22m 1 problem (esc[31m1 erroresc[39m, esc[33m0 warnings esc[39m) 原因: …...

图论17(Leetcode864.获取所有钥匙的最短路径)
用二进制表示获得的钥匙,假设n钥匙个数 000000000代表没有钥匙,0000000001代表有idx为1的钥匙,0000000011代表有idx1,2的钥匙 (这方法巧妙又复杂.. 代码: class Solution {static int[][] dirs {{-1,0}…...

vue 脚手架 入门 记录
vue 脚手架 入门 记录 以管理员身份运行PowerShell执行:get-ExecutionPolicy,回复Restricted,表示状态是禁止的 3.执行:set-ExecutionPolicy RemoteSigned 4.选择Y 注意:一定要以管理员的身份运行PowerShellÿ…...

汽车租赁系统演示租车小程序H5开发
一款适合汽车租赁业务的程序系统,支持在线支付、一键续租、在线签名。 为了方便用户端方便租车和查看已租车辆的信息和费用,支持上门取车和到店取车两种模式,支持待付款、待取车、待还车、订单管理、已完成、全部订单等订单状态查看和处理功…...

【MySQL】 MySQL 更新数据机制
MySQL 更新数据机制 一、问题描述 假设我们有这样一张表,且包含一条记录: CREATE TABLE mytest ( id int(11) NOT NULL, c1 int(11) DEFAULT NULL, c2 int(11) DEFAULT NULL, c3 int(11) DEFAULT NULL, PRIMARY KEY (id), KEY c1 (c1), KEY c2 (c2) 包…...