FileInputStream(文件字节输入流)
一、java.io.FileInputStream
- 文件字节输入流,任何类型的文件都可以采用这个流来读
- 以字节的方式,完成输入的操作,完成读的操作(硬盘—> 内存)
- 读取数据的原理(硬盘—>内存)
Java程序–>JVM(Java虚拟机)–>OS(操作系统)–>OS调用读取数据的方法–>读取文件 - 字节输入流的使用步骤
- 创建一个FileInputStream对象,构造方法中绑定要读取的数据源
- 调用FileInputStream对象中的方法read,读取文件
- 释放资源(流使用会占用一定的内存,使用完毕后要把内存清空,提高程序的效率)
1. 测试用例初步(开发不用)
- 在D:\Test下创建temp文件,文件内容:abcdef
- 读取字节:read方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回-1
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:/Test/temp");
int readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\Test\\temp");
int readData = 0;
while((readData = fis.read()) != -1){
System.out.println(readData);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 以上两个程序一次读取一个字节byte,这样内存和硬盘交互太频繁,基本上时间/资源都耗费在交互上面了
- 对以上两个程序进行改进,一次读取多个字节
2. 测试用例改进
- 使用字节数组读取int read(byte[] bytes),一次最多读取 bytes.length 个字节,即往byte[]数组当中读,减少硬盘和内存的交互,提高程序的执行效率,读取到末尾时,返回-1
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\Test\\temp");
byte[] bytes = new byte[4];
int readCount = fis.read(bytes);
System.out.println(readCount);
System.out.println(new String(bytes, 0, readCount));
readCount = fis.read(bytes);
System.out.println(readCount);
System.out.println(new String(bytes, 0, readCount));
readCount = fis.read(bytes);
System.out.println(readCount);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3. 测试用例最终(掌握)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\Test\\temp");
byte[] bytes = new byte[4];
int readCount = 0;
while ((readCount = fis.read(bytes)) != -1) {
System.out.print(new String(bytes, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
二、FileInputStream类的其它常用方法
1. int available()
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:/Test/temp");
System.out.println("总字节数量:" + fis.available());
int readByte = fis.read();
System.out.println("剩下多少个字节没有读:" + fis.available());
byte[] bytes = new byte[fis.available()];
int readCount = fis.read(bytes);
System.out.println(new String(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2. long skip(long n)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:/Test/temp");
fis.skip(4);
System.out.println(fis.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}