当前位置:网站首页>深入理解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();
}
}
}
}
}
边栏推荐
猜你喜欢

文件上传很难搞?10分钟带你学会阿里云OSS对象存储

MySQL索引

全面认识二极管,一篇文章就够了

Data Governance: The Evolution of Data Integration and Application Patterns

嵌入式Qt-做一个秒表

redis summary_distributed cache

Go 语言快速入门指南: 介绍及安装

多聚体/壳聚糖修饰白蛋白纳米球/mPEG-HSA聚乙二醇人血清白蛋白纳米球的制备与研究

灵动微电子发布低功耗 MM32L0130 系列 MCU 产品

Taking advantage of cloud-network integration, e-Surfing Cloud has paved the way for digital transformation for government and enterprises
随机推荐
文件上传很难搞?10分钟带你学会阿里云OSS对象存储
【案例】2D变换-旋转动画
Mysql和Redis如何保证数据一致性
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (5) Task Book
NeRF:火爆科研圈的三维重建技术大揭秘
红队实战靶场ATT&CK(一)
Flink Learning 9: Configure the idea to develop the flink-Scala program environment
一朵“云“如何带来产业新变革
如何应对机器身份带来的安全风险
Google Earth Engine APP—— 一个不用写代码可以直接下载相应区域的1984-2021年的GIF遥感影像动态图
面试官:可以谈谈乐观锁和悲观锁吗
Data Governance: The Evolution of Data Integration and Application Patterns
潮玩的“第二春”,在哪?
攻防世界-favorite_number
ffmpeg cannot find libx264 after compilation
golang刷leetcode 经典(4) 实现跳表
嵌入式Qt-做一个秒表
百问百答第49期:极客有约——国内可观测领域SaaS产品的发展前景
MySQL基本操作和基于MySQL基本操作的综合实例项目
织梦提示信息提示框美化