java 输入字节流
来源:互联网 发布:库里15 16赛季数据 编辑:程序博客网 时间:2024/06/07 06:35
一、知识点
1、输入字节流:
- InputStream 所有输入字节流的基类 (抽象类)
- FileInputStream 读取文件数据的输入字节流
2、使用FileInputStream读取文件数据的步骤:
① 找到目标文件② 建立数据的输入通道③ 读取文件中的数据④ 关闭资源
二、使用类
File类
- 构造方法
FileInputStream类
构造方法 FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。int read()
从此输入流中读取一个数据字节。int read(byte[] b)
从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。int read(byte[] b, int off, int len)
从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。void close()
关闭此文件输入流并释放与此流有关的所有系统资源。
三、代码
public class FileInputStream { public static void main(String[] args) throws IOException { readTest1(); readTest2(); readTest3(); readTest4(); } //方式1:不使用循环读取 //缺陷: 无法完整读取一个文件的数据(淘汰) public static void readTest1() throws IOException { //1. 找到目标文件 File file = new File("F:\\a.txt"); //2.建立数据的输入通道。 FileInputStream fileInputStream = new FileInputStream(file); //3.读取文件中的数据 int content = fileInputStream.read(); // read() 读取一个字节的数据,把读取的数据返回。 System.out.println("读到的内容是:"+ (char)content); //4.关闭资源 fileInputStream.close(); } //方式2:使用循环读取文件的数据 //缺陷:效率低 public static void readTest2() throws IOException { //1.找到目标文件 File file = new File("F:\\天涯古巷\\1.jpg"); //2.建立数据的输入通道 FileInputStream fileInputStream = new FileInputStream(file); //3.读取文件的数据 int content = 0; //声明该变量用于存储读取到的数据 while((content = fileInputStream.read())!=-1) { System.out.print((char)content); } //4.关闭资源 fileInputStream.close(); } //方式3:使用缓冲数组读取 //缺点: 无法完整读取一个文件的数据(淘汰) public static void readTest3() throws IOException { //1.找到目标文件 File file = new File("F:\\a.txt"); //2.建立数据的输入通道 FileInputStream fileInputStream = new FileInputStream(file); //3.建立缓冲字节数组,读取文件的数据。 byte[] buf = new byte[1024]; int length = fileInputStream.read(buf); //如果使用read读取数据传入字节数组,那么数据是存储到字节数组中的,而这时候read方法的返回值是表示的是本次读取了几个字节数据到字节数组中。 System.out.println("length:"+ length); //使用字节数组构建字符串 String content = new String(buf,0,length); System.out.println("内容:"+ content); //4.关闭资源 fileInputStream.close(); } //方式4:使用缓冲数组配合循环一起读取。(推荐) public static void readTest4() throws IOException { //1.找到目标文件 File file = new File("F:\\天涯古巷\\1.jpg"); //2.建立数据的输入通道 FileInputStream fileInputStream = new FileInputStream(file); //3.建立缓冲数组配合循环读取文件的数据。 int length = 0; //保存每次读取到的字节个数。 byte[] buf = new byte[1024]; //存储读取到的数据 //缓冲数组的长度一般是1024的倍数,因为与计算机的处理单位。理论上缓冲数组越大,效率越高 while((length = fileInputStream.read(buf))!=-1) { // read方法如果读取到了文件的末尾,那么会返回-1表示。 System.out.print(new String(buf,0,length)); } //4.关闭资源 fileInputStream.close(); }}
阅读全文
0 0
- java 输入字节流
- java 字节输入流、字节输出流、字节缓冲流
- java-IO流1(输入字节流)
- Java(文件输入/输出字节流)
- java的输入字节流 FileInputStream类
- java字节文件输入流读文件
- Java之字节输入流InputStream
- Java 字节输入流的一些方法
- Java字节流学习记录(3)--字节流InputStream及其字节输入流的子类
- 字节输入/输出流
- 字节输入流
- 文件字节输入流
- 输入流转字节流
- 字节输入流(FileInputStream)
- 字节输入 输出流
- 字节输入流
- 缓冲字节输入流
- java 输入流转字节数组
- 第十一周项目二(1)
- ireport正在装入缓存的对象
- Android----webview 去除网页标题
- Python数据分析之pandas学习
- tomcat启动项目报错:Java.lang.classnotfoundexception filterconfig
- java 输入字节流
- Windows Server 2008 系列之AD+DNS域安装
- git基本操作
- 探索深入理解java虚拟机之虚拟机类文件结构(5)
- 在Word2013中设置文本格式与对齐方式
- 【C#】正则表达式
- qml学习笔记(六):可视化元素Text、Window
- C语言标准库函数qsort那点小事———转载而来
- 第十一周项目4(1)-利用遍历思想求解图问题