package 草稿;
//下面四个都是抽象类
//InputStream 字节输入流
//OutputStream 字节输出流
//Reader //字符输入流
//Writer //字符输出流
import org.junit.jupiter.api.Test;
import java.io.*;
public class 字节输入流 {
public static void main(String[] args) {
}
//单个字节的读取
@Test
public void readFile01(){
String filePath="C:\\Users\\ASUS\\Desktop\\19\\hello.txt";
int readData = 0;
FileInputStream fileInputStream=null;
try {
fileInputStream = new FileInputStream(filePath);
//从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止。
//如果返回-1,表示读取完毕
while ((readData = fileInputStream.read()) != -1){
System.out.print((char) readData);//转成char显示
}
} catch (Exception e) {
System.out.println("错误");
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//使用read(byte[] b)来多个字节读取
@Test
public void readFile02(){
String filePath="C:\\Users\\ASUS\\Desktop\\19\\hello.txt";
//字节数组
byte[] buf= new byte[8];//一次读取8个字节。
int readlen=0;
FileInputStream fileInputStream=null;
try {
fileInputStream = new FileInputStream(filePath);
//从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止。
//如果返回-1,表示读取完毕
//如果读取正常,返回实际读取的字节数
while ((readlen=fileInputStream.read(buf)) != -1){
System.out.print(new String(buf,0,readlen));//转成char显示
}
} catch (Exception e) {
System.out.println("错误");
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
版权声明:本文为qq_69958724原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。