FileInputStream
package com.hspedu;
import org.junit.jupiter.api.Test;
import java.io.*;
/**
* @author: guorui fu
* @versiion: 1.0
*/
public class UseInputStream {
public static void main(String[] args) {
}
@Test
public void readStream1() {
//定义一个字节数组
byte[] buf = new byte[8];
String fileName = "e:\\news1.text";
int readLength = 0;
//存在关闭的问题,先指控,然后在finally里关闭
FileInputStream fileInputStream = null;
try {
//创建输入流,获取地址
fileInputStream = new FileInputStream(fileName);
//使用read写入方法,写入创建的buf数组中,该方法返回length(写入的字符长度)
while ((readLength = fileInputStream.read(buf)) != -1) {
//用字符串接收
System.out.print(new String(buf,0,readLength));
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}