当前位置: 首页 > news >正文

FileInputStream文件字节输入流

一.概念

以内存为基准,把磁盘文件中的数据以字节形式读入内存中

二.构造器

public FileInputStream(File file)

public FileInputStream(String pathname)

这两个都是创建字节输入流管道与源文件接通

三.方法

public int read() :每次读取一个字节返回,如果发现没有数据可读,返回-1。

public int read(byte[] buffer) :每次用一个字节数组读取数据,返回字节数组读取了多少字节,如果发现没有数据可读,返回-1.

四.执行

方法一:一个一个字节读

1.代码
package org.example;import java.io.*;public class day05 {public static void main(String[] args) throws IOException {//1.创建文件字节输入流管道与源文件接通:两种方法都行InputStream f1 = new FileInputStream(new File("D:\\temp\\day05\\a.txt"));InputStream f2 = new FileInputStream("D:\\temp\\day05\\a.txt");//2.读取文件的字节数据int b1 = f1.read();System.out.println(b1);System.out.println((char) b1);int b2 = f1.read();System.out.println(b2);System.out.println((char) b2);int b3 = f1.read();System.out.println(b3);}
}
2.结果

 

上面代码一个一个字节读太麻烦了,而且读取汉字会乱码,下面进行优化

方法二:循环读

1.代码
package org.example;import java.io.*;public class day05 {public static void main(String[] args) throws IOException {InputStream f1 = new FileInputStream("D:\\temp\\day05\\b.txt");int b; //用于记住读取的字节while((b = f1.read()) != -1){System.out.print((char)b);}f1.close();}
}

上面代码读取性能很差,且读取汉字会乱码,需要进一步改进 ;流使用完必须要关闭,释放系统资源。

2.结果

方法三:每次读取多个字节

1.代码
package org.example;import java.io.*;public class day05 {public static void main(String[] args) throws IOException {//b.txt内容:abcdefgInputStream f1 = new FileInputStream("D:\\temp\\day05\\b.txt");//开始读取文件中的字节数据,每次读取多个字节byte[] buffer = new byte[4];int len = f1.read(buffer);String s = new String(buffer);System.out.println(s);System.out.println("读取的字节数"+len);int len2 = f1.read(buffer);String s2 = new String(buffer);System.out.println(s2);System.out.println("读取的字节数"+len2);f1.close();}
}
2.结果

正常情况下,第二次读取的结果应该是efg而不是efgd

3.改进
package org.example;import java.io.*;public class day05 {public static void main(String[] args) throws IOException {//b.txt内容:abcdefgInputStream f1 = new FileInputStream("D:\\temp\\day05\\b.txt");//开始读取文件中的字节数据,每次读取多个字节byte[] buffer = new byte[4];int len = f1.read(buffer);String s = new String(buffer);System.out.println(s);System.out.println("读取的字节数"+len);int len2 = f1.read(buffer);String s2 = new String(buffer,0,len2);System.out.println(s2);System.out.println("读取的字节数"+len2);f1.close();}
}
4.结果 

这个代码有待优化,用循环进一步优化

 方法四:循环读取

1.代码
package org.example;import java.io.*;public class day05 {public static void main(String[] args) throws IOException {//b.txt内容:abcdefgInputStream f1 = new FileInputStream("D:\\temp\\day05\\b.txt");//开始读取文件中的字节数据,每次读取多个字节byte[] buffer = new byte[4];int len;while ((len = f1.read(buffer)) != -1) {String s = new String(buffer, 0, len);System.out.print(s);}f1.close();}
}
2.结果

 

五.问题 

上面代码读取性能提升了,但依旧在读取汉字上会产生乱码

解决方案一:定义一个与文件一样大的字节数组,一次性读取完文件的全部字节(不推荐)

 方法1

1.代码
package org.example;import java.io.*;public class day05 {public static void main(String[] args) throws IOException {//c.txt内容:我们在一起abcdInputStream f1 = new FileInputStream("D:\\temp\\day05\\c.txt");//这里的19可以用f1.length()获取byte[] buffer = new byte[19];int len;while ((len = f1.read(buffer)) != -1) {String s = new String(buffer, 0, len);System.out.print(s);}f1.close();}
}
2.结果

 

 方法2

1.代码
package org.example;import java.io.*;public class day05 {public static void main(String[] args) throws IOException {//c.txt内容:我们在一起abcdInputStream f1 = new FileInputStream("D:\\temp\\day05\\c.txt");final byte[] bytes = f1.readAllBytes();System.out.println(new String(bytes));}
}
2.结果

上面代码还有待优化,万一文件特别大,用readAllBytes()会抛出异常。

 

http://www.lryc.cn/news/209454.html

相关文章:

  • 【Qt】窗口和对话框区别、主窗口和二级窗口区别、QMainWindow和QDialog区别
  • Python参数种类介绍
  • react事件机制
  • JAVA删除excel指定列
  • Netty编码器和解码器
  • 大语言模型(LLM)综述(三):大语言模型预训练的进展
  • 如何在Node.js中使用环境变量或命令行参数来设置HTTP爬虫ip?
  • VMware打开共享虚拟机后找不到/mnt/hgfs/文件夹,以及不能拖拽/复制粘贴等操作,ubuntu不能安装VMware tools
  • pytorch 入门 (五)案例三:乳腺癌识别识别-VGG16实现
  • 【QT开发(14)】QT P2P chat 聊天
  • 解决adb root命令时错误 adbd cannot run as root in production builds
  • 操作系统中套接字和设备独立性软件的关系
  • C++ Qt/VTK装配体组成联动连接杆
  • File文件查找
  • 小程序 wxml2canvas开发文档
  • SpringCloud微服务 【实用篇】| 认识微服务
  • Csdn文章编写参考案例
  • Jmeter性能测试:高并发分布式性能测试
  • 2015年亚太杯APMCM数学建模大赛B题城市公共交通服务水平动态评价模型求解全过程文档及程序
  • CCF CSP认证历年题目自练 Day40
  • 闲聊一下写技术博客的一些感想
  • 单片机为什么一直用C语言,不用其他编程语言?
  • 利用HTTP2,新型DDoS攻击峰值破纪录
  • android鼠标滚轮事件监听方法
  • 【C语言|关键字】C语言32个关键字详解(4)——其他(typedef、sizeof)
  • Hafnium简介和构建
  • 2023年香水行业数据分析:国人用香需求升级,高端香水高速增长
  • 这可能是最简单的Page Object库
  • 论文阅读——BERT
  • 竞赛 深度学习人体跌倒检测 -yolo 机器视觉 opencv python