关于java:关于FileInputStream文件字节输入流的使用

6次阅读

共计 2968 个字符,预计需要花费 8 分钟才能阅读完成。

对于 FileInputStream 文件字节输出流:

FileInputStream,称为文件字节输出流,属于万能流,一次读一个字节 byte,什么文件都能读。

也称为 Read(读),从硬盘 到 内存输出文件数据。

测试代码:

import java.io.*;

public class Test02 {

public static void main(String[] args) {

// 创立文件字节输出流对象

FileInputStream fis = null;

// 流操作会抛出异样,属于编译时异样,须要咱们事后解决,main 办法中的异样通常进行捕获,不要上抛异样

try {

// F:\JavaCode\JavaSE\IOtest\test02.txt,文件地址,这种地址属于绝对路径

// 文本内容:abc

fis = new FileInputStream(“F:\\JavaCode\\JavaSE\\IOtest\\test02.txt”); // 留神写 2 个 \\,2 个 \\ 示意 1 个 \

//fis = new FileInputStream(“F:/JavaCode/JavaSE/IOtest/test02.txt”); // 也能够写这种 /,这种 / 没有本义的意思

// 开始 Read(读)

/*

int readData = fis.read(); // 返回的是第一个字节的 ASCII 编码值

System.out.println(readData); // 97 读的是 ’a’ 的字节

// 再读下一个字节

readData = fis.read();

System.out.println(readData); //98 读 b

// 再读下一个字节

readData = fis.read();

System.out.println(readData); //99 读 c

// 再读下一个字节

readData = fis.read(); // 当没有数据时,read() 办法返回 -1

System.out.println(readData); //-1

*/

// 以上读的操作也能够用循环管制

int readData = 0;

while ((readData = fis.read()) != -1) {

System.out.print(readData + ” “); // 97 98 99

}

System.out.println();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {// finally 语句块中的代码肯定会执行,确保流用完后敞开

if (fis != null) {// 只有 fis 不为空的时候,才须要敞开通道

try {

fis.close(); // close() 办法会抛出编译时异样,持续 try..catch

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

对 FileInputStream 的 read() 办法的相熟:
外汇出入金流程 https://www.fx61.com/support

如果应用 read() 每次都只能读一个字节,这样的话硬盘和内存之间交互太频繁,浪费时间。

能够思考应用 read(byte[] b) 办法,一次最多读取 b.length 个字节。办法返回值是理论读取到的字节数量。

留神:是最多读取到 b.length 个字节,当读取到文件开端时,有可能不够 b.length 个。

如果文件读取完了,再次读取就会返回 - 1 了。

测试代码:

import java.io.*;

public class Test03 {

public static void main(String[] args) {

FileInputStream fis = null;

try {

// 应用相对路径,IOtest\test03.txt,是以后文件下的

// 留神:IDEA 中的以后门路是 project 工程这个根文件

fis = new FileInputStream(“IOtest\\test03.txt”);

// 先揭示一下文本内容为:abcdef

// 创立 byte[] 数组

byte[] bytes = new byte[4]; // 指定一次最多读取 4 个字节

// 第一次读取

/*

int readCount = fis.read(bytes);

System.out.println(readCount); // 4

String strData = new String(bytes, 0, readCount); // 将读取到的 bytes 数组转换成字符串

System.out.println(strData); //abcd

// 第二次读取

readCount = fis.read(bytes);

System.out.println(readCount); // 2

strData = new String(bytes, 0, readCount);

System.out.println(strData); //ef

// 第三次读取

readCount = fis.read(bytes);

System.out.println(readCount); // -1 示意曾经读取完了

*/

// 以上读取应该用循环管制

int readCount = 0;

while ((readCount = fis.read(bytes)) != -1) {

System.out.print(new String(bytes, 0, readCount)); // abcdef

}

System.out.println();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

对于 FileInputStream 类的罕用办法:

(1)public FileInputStream(String name);

构造方法,创立文件输出流对象,连贯到指定文件名。

(1)int read();

一次读取一个字节,返回改字节的 ASCII 码值。文件读完后返回 -1.

(2)int read(byte[] b);

一次最多读取 b.length 个字节,返回理论读取字节的数量。文件读完后返回 -1。

(3)void close();

敞开以后流的所有管道,开释资源。

所有流用完之后,肯定记得敞开,开释资源。

通常 close() 办法放在 finally 语句块当中,来确保流用完后会敞开。

(4)int available();

返回流当中残余的没有读取的字节数量。文件读完后返回 0。

当文件不大时,能够一次性读完文件。

byte[] bytes = new byte[fis.available()]; // 间接创立对应大小的 byte[] 数组

int readeCount = fis.read(bytes);

System.out.println(new String(bytes, 0, readCount)); // 如果是文本文件,能够自接打印查看

(5)long skip(long n);

跳过 n 个字节不读取。

有的文件可能有固定的格局,能够跳过这些不必要的字节数,来达到进步读取的效率。

正文完
 0