当前位置:网站首页>深入理解IO流(第一篇)
深入理解IO流(第一篇)
2022-08-02 17:48:00 【InfoQ】
readDate = fis.read();
System.out.println(readDate); //98
readDate = fis.read();
System.out.println(readDate); //99
readDate = fis.read();
System.out.println(readDate); //-1;最终没数据了,就返回-1
//2、循环读
while(true){
int readDate1 = fis.read();
if(readDate1 == -1){ // 没有数据返回的是-1
break;
}
System.out.println(readDate1);
}
//3、优化while
int readDate1 = 0;
while((readDate1 = fis.read()) != -1){
System.out.println(readDate1);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { //read时候,补充的异常处理
e.printStackTrace();
} finally {
// 加上finally关键字,无论最终有没有异常,都需要关闭这个流
// 在finally语句块当中确保流一定关闭
if (fis != null) { //生成这个的快捷键ifn
// 关闭流的前提,流不是null;是空没必要关闭
try {
fis.close(); //有异常,try...catch
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 开始读,采用byte数组,一次读取多个字节。最多读取“数组.length”个字节。
byte[] bytes = new byte[4]; 准备一个4个长度的byte数组,一次最多读取4个字节
/* 1、普通打印
int readCount = fis.read(bytes); // 返回的是当前的读取到的字节数量。(不是字节本身)
System.out.println(readCount); // 4;第一次读到了4个字节
//System.out.println(new String(bytes)); //abcd,将字符数组全部转换成字符串
// 实际上应该读到多少个,就转换多少个
System.out.println(new String(bytes,0,readCount)); // abcd
readCount = fis.read(bytes);
System.out.println(readCount); // 2;第二次只能读取到2个字节
//System.out.println(new String(bytes)); //efcd,将字符数组全部转换成字符串,这里就出了问题
System.out.println(new String(bytes,0,readCount)); //ef
readCount = fis.read(bytes);
System.out.println(readCount); //1个字节都没有读取到返回-1*/
//2、 写成循环
while(true){
int readCount = fis.read(bytes);
if(readCount == -1){
break;
}
System.out.print(new String(bytes,0,readCount)); // abcdef
}
//3、 代码优化
int readCount = 0;
while((readCount = fis.read(bytes)) != -1){
// 将读取到的将字符数组全部转换成字符串
System.out.print(new String(bytes,0,readCount)); // abcdef
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 2、skip方法,跳过几个字节
fis = new FileInputStream("tempfile.txt");
// 跳过3个字节
fis.skip(3);
System.out.println(fis.read()); //100(d)
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//2、以追加的方式在文件末尾写入。不会清空原文件内容。
fos = new FileOutputStream("myfile",true);
//3、开始写,写到数组里
byte[] bytes = {97,98,99,100};
fos.write(bytes); // 写进去abcd
//4、将byte数组的一部分写出
fos.write(bytes,0,2); //写进去ab
//5、写一个字符串,然后把字符串转换成byte数组
String s = "我是一个中国人!";
//将一个字符串转换成byte数组
byte[] byts = s.getBytes();
fos.write(byts); //写进去“我是一个中国人!”
//6、写完之后一定要刷新
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//3、 最核心的部分:一边读,一边写
byte[] bytes = new byte[1024 * 1024]; // 1MB(一次最多拷贝1MB。)
int readCount = 0;
while((readCount = fis.read(bytes)) != -1) {
fos.write(bytes, 0, readCount);
}
//4、 刷新,输出流最后要刷新
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 分开try,不要一起try。
// 一起try的时候,其中一个出现异常,可能会影响到另一个流的关闭。
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 第一种方法
int readCount = 0;
while((readCount = reader.read(chars)) != -1){
System.out.print(new String(chars,0,readCount));
}
// 补充
// reader.read(chars); // 往char数组中读
// 按照字符的方式读取,一次读取一个字符
for(char c :chars){
System.out.println(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//2、 开始写
char[] chars = {'我','是','中','国','人'};
// 写整个数组的内容
writer.write(chars);
// 也可以只写数组的一部分
writer.write(chars,0,2);
// 后面也可以直接跟字符串
writer.write("\n"); // 换行
writer.write("我很骄傲");
//3、 刷新
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
{
try {
// 创建字符输入流
reader = new FileReader("file");
// 创建字符输出流
writer = new FileWriter("myfile");
// 边读边写
char[] chars = new char[1024*1024]; // 1MB
int readCount = 0;
while((readCount = reader.read(chars)) != -1){
writer.write(chars,0,readCount);
}
// 刷新
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
边栏推荐
- 阿波罗 planning代码-modules\planning\lattice\trajectory_generation\PiecewiseBrakingTrajectoryGenerator类详解
- SQL 正则解析手机号码提供商
- ES: Promise的基本用法
- The days of patching are more difficult than the days of writing code
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(8)毕业设计论文模板
- Playing in the cloud | The key technology of Tianyi cloud object storage ZOS high availability is revealed
- NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle
- IReport常见问题及处理方法
- 玩转云端 | 天翼云对象存储ZOS高可用的关键技术揭秘
- 【秒杀办法】根据二叉树的先序遍历、中序遍历、后序遍历快速创建二叉树
猜你喜欢
腾讯架构师是如何解释:Redis高性能通信的原理(精华版)
MySQL索引
MySQL命令(命令行方式,而非图形界面方式)
今年上半年,我国公路建设总体形势持续向好
天翼云4.0来了!千城万池,无所不至!
MySQL表的约束
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Works Mini Program Graduation Design Finished Work (6) Question Opening Reply PPT
一朵“云“如何带来产业新变革
HDF驱动框架的API(3)
灵动微电子发布低功耗 MM32L0130 系列 MCU 产品
随机推荐
cpolar应用实例之多设备数据采集
CUDA+Pycharm-gpu版本+Anaconda安装
织梦提示信息提示框美化
LiveGBS国标GB/T28181流媒体平台支持主子码流切换主码流stream/streamprofile
面试官:可以谈谈乐观锁和悲观锁吗
如何构建准实时数仓?
Ubuntu系统下用docker安装oracle
How Tencent architects explained: The principle of Redis high-performance communication (essential version)
记一次 .NET 某工控自动化控制系统 卡死分析
Flink学习9:配置idea开发flink-Scala程序环境
【案例】2D变换-旋转动画
Redis总结_实战篇
9月起中国给予多哥等16国98%税目产品零关税待遇
redis summary_distributed cache
企业云成本管控,你真的做对了吗?
千万级QPS下服务如何才能平滑启动
54.【system系统互动函数大总结】
本地MSE播放fragment mp4服务
golang 源码分析(39)hystrix-go
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Works Mini Program Graduation Design Finished Work (6) Question Opening Reply PPT