当前位置:网站首页>深入理解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();
}
}
}
}
}
边栏推荐
- Taking advantage of cloud-network integration, e-Surfing Cloud has paved the way for digital transformation for government and enterprises
- 深圳地铁16号线二期进入盾构施工阶段,首台盾构机顺利始发
- 打补丁的日子,比写代码的日子难熬多了
- ffmpeg编译后找不到libx264
- 2021年下半年软件设计师上午真题
- KunlunBase 1.0 is released!
- What is the difference between erp system and wms system
- SQL 正则解析手机号码提供商
- MySQL命令(命令行方式,而非图形界面方式)
- 究极异常处理逻辑——多层次异常的处理顺序
猜你喜欢

Redis总结_实战篇

企业云成本管控,你真的做对了吗?

攻防世界-favorite_number

什么是SVN(Subversion)?

vulnhub W34kn3ss: 1

Smart Microelectronics Releases Low-Power MM32L0130 Series MCU Products

NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle

MySQL基本语法

Navicat 连接Oracle时提示oracle library is not loaded的问题解决

MySQL基本操作和基于MySQL基本操作的综合实例项目
随机推荐
2022最新版SSM源码分析:一套教程助你深入理解底层原理,提高核心竞争力!
Dream weaving prompt information prompt box beautification
牛津硕士进碳圈,高瓴红杉经纬一起投了
cpolar应用实例之多设备数据采集
新特性解读 | MySQL 8.0 GIPK 不可见主键
54.【system系统互动函数大总结】
危及安全的常见物联网攻击有哪些?
redis总结_多级缓存
ffmpeg编译后找不到libx264
MySQL基本查询和运算符
POE交换机全方位解读(下)
天翼云4.0来了!千城万池,无所不至!
golang刷leetcode 经典(1) LRU缓存机制
小程序毕设作品之微信体育馆预约小程序毕业设计成品(5)任务书
C# 术语
阿波罗 planning代码-modules\planning\lattice\trajectory_generation\PiecewiseBrakingTrajectoryGenerator类详解
What is the difference between erp system and wms system
How to ensure the security of smart factories?
LiveGBS国标GB/T28181流媒体平台支持主子码流切换主码流stream/streamprofile
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (5) Task Book