一.概述
java.io.InputStream是java标准库提供的最基本的输入流,它是一个抽象类,是所有输入流的超类。其中有一个read()方法,会读取输入流的下一个字节,并返回字节表示的int值(0~255)。如果已读到末尾,返回-1表示不能继续读取了。
二.FileInputStream
InputStream的一个子类。
我们通过查看FileInputStream的官方文档,我们主要看第三个构造函数。
主要看一下read()函数。
package com.sdnu.project1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author Beyong
* @Description FileInputStream
* @date 2021/05/23 17:52
*/
public class Demo1 {
public static void main(String[] args) {
FileInputStream fileDate = null;
try{
fileDate = new FileInputStream("d:\\test.txt");
System.out.println(fileDate.read());
System.out.println(fileDate.read());
System.out.println(fileDate.read());
System.out.println(fileDate.read());
System.out.println(fileDate.read());
System.out.println(fileDate.read());
System.out.println(fileDate.read());
System.out.println(fileDate.read());
System.out.println(fileDate.read());
System.out.println(fileDate.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if( fileDate != null){
try{
fileDate.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
改进版,循环读
package com.sdnu.project1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author Beyong
* @Description FileInputStream
* @date 2021/05/23 17:52
*/
public class Demo1 {
public static void main(String[] args) {
FileInputStream fileDate = null;
/* 改进*/
try{
fileDate = new FileInputStream("d:\\test.txt");
int temp = 0;
while((temp = fileDate.read()) != -1){
System.out.println(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if( fileDate != null){
try{
fileDate.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
总结:FileInputStream用于从文件读取数据
三.缓冲区管理
(1)开辟缓冲区读取
上面我们虽然实现了文件以流的方式读取,但是每一次一个字节效率低,于是我们想到开辟一个缓冲区,一次读取多个字节。
txt文件:
程序:
package com.sdnu.project1;
import java.io.*;
/**
* @author Beyong
* @Description buffer
* @date 2021/05/24 13:46
*/
public class Demo2 {
public static void main(String[] args) {
FileInputStream file = null;
try{
file = new FileInputStream("D:\\test.txt");
byte[] buffer = new byte[4];
int readCount;
/*第一次*/
readCount = file.read(buffer);
System.out.println(readCount);
/*第二次*/
readCount = file.read(buffer);
System.out.println(buffer);
/*第三次*/
readCount = file.read(buffer);
System.out.println(buffer);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
结果:
我们通过byte[] buffer = new byte[4];
在内存当中开辟了4个字节大小空间作为我们的缓冲区,然后每一次读取4个字节。
(注意,file.read(buffer)返回的结果是读取的字节数)
(2)转换字符串
输出改为:
System.out.println(new String(buffer));
我们通过new String()可以将byte转换成字符串对象。
三次读取内存变化:
(3)最终版
package com.sdnu.project1;
import java.io.*;
/**
* @author Beyong
* @Description buffer
* @date 2021/05/24 13:46
*/
public class Demo2 {
public static void main(String[] args) {
FileInputStream file = null;
try{
file = new FileInputStream("D:\\test.txt");
byte[] buffer = new byte[4];
while(true){
int readCount = file.read(buffer);
if(readCount == -1){
break;
}
System.out.println(new String(buffer, 0, readCount));
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
有多少读多少。
改进:
package com.sdnu.project1;
import java.io.*;
/**
* @author Beyong
* @Description buffer
* @date 2021/05/24 13:46
*/
public class Demo2 {
public static void main(String[] args) {
FileInputStream file = null;
try{
file = new FileInputStream("D:\\test.txt");
System.out.println("总字节数为 :" + file.available());
byte[] buffer = new byte[file.available()];
int readCount = file.read(buffer);
System.out.println("读进去的字节数为 :" + readCount);
System.out.println(new String(buffer));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
作者:Beyong
出处:Beyong博客
github地址:https://github.com/beyong2019
本博客中未标明转载的文章归作者Beyong有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。