Java中的IO流之输入流|乐字节

时间:2023-12-23 12:08:32

亲爱的乐字节的小伙伴们,小乐又来分享Java技术文章了。上一篇写到了IO流,这篇文章着重 谈谈输入流,再下次再说输出流。

点击回顾上一篇:乐字节Java之file、IO流基础知识和操作步骤

一、 输入流

字节流和字符流的操作方式几乎完全一样,只是操作的数据单元不同而已 。字节流可

以操作所有文件,字符流仅操作纯文本。

1、抽象类:InputStream 和 Reader

InputStream和Reader是所有输入流的基类,它们是两个抽象类,是所有输入流的模版,其中定义的方法在所有输入流中都可以使用。

在InputStream里包含如下三个方法:

Java中的IO流之输入流|乐字节

在Reader中包含如下三个方法:

Java中的IO流之输入流|乐字节

对比InputStream和Reader 所提供的方法,可以看出这两个基类的功能基本相似。返回结果为-1 时表明到了输入流的结束点。 InputStream 和 Reade 都是抽象的,不能直接创建它们的实例,可以使用它们的子类。

2、文件节点类: FileInputStream 和 FileReader

FileInputStream 和 FileReader,它们都是节点流,直接和指定文件关联。 操作方式

基本一致。

1)、单个字节读取

以FileInputStream为例:

public class SingleFileRead {
public static void main(String[] args) {
// 1、建立联系 File对象
File file = new File("f:/IO/test.txt");
// 2、选择流
InputStream in = null;// 提升作用域
try {
in = new FileInputStream(file);
// 3、操作 单个字节读取
long fileLength = file.length(); // 接收实际读取的字节数
// 计数器
System.out.println(fileLength);
long num = 0;
// 循环读取
while (num < fileLength) {
char ch = (char) in.read();
System.out.println(ch);
num++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件不存在,不能进行下一步操作");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("读取文件失败");
} finally {
try {
// 4、释放资料
if (in != null) {
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("关闭文件输入流失败");
}
}
}
} 乐字节原创

2)、批量读取(字节|字符重点)

public class ReadFile {
public static void main(String[] args) {
//1、字节读取:建立联系 File对象
File file=new File("f:/IO/test.txt");
//2、选择流
InputStream in=null;//提升作用域
try {
in=new FileInputStream(file);
//3、操作 不断读取 缓冲数组
byte[]car=new byte[1024];
int len=0; //接收实际读取的大小
//循环读取
while(-1!=(len=in.read(car))){
//输出,字节数组转成字符串
String info=new String(car,0,len);
System.out.println(info);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件不存在");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("读取文件失败");
}finally{
try {
//4、释放资料
if(in!=null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("关闭文件输入流失败");
}
}
}
}
//字符读取1、创建源
File src=new File("f:/char.txt");
//2、选择流
Reader reader=new FileReader(src);
//3、读取操作
char[] flush=new char[1024];
int len=0;
while(-1!=(len=reader.read(flush))){
//字符数组转换为字符串
String str=new String(flush,0,len);
System.out.println(str);
}
//4、释放资源
reader.close();
乐字节原创

3、缓冲处理类:BufferedInputStream和 BufferedReader(重点)

缓冲提高性能: 字节流直接套上即可;字符缓冲流 +新增方法(不能使用多态)

//1、创建源,建立联系
File src =new File("test.txt");
//2、选择缓冲流
InputStream is =new BufferedInputStream(new FileInputStream(src)); //3、操作 : 多个读取
byte[] car =new byte[2];
int len =0;
while(-1!=(len=is.read(car))){
//获取数组的内容 字节数组转字符串 new String(字节数组,0,length)
System.out.println(new String(car,0,len));
}
//4、释放资源
is.close();
//创建源:
File src =new File("test.txt");
//使用字符缓冲流 提高性能读取文件 +新增方法(不能使用多态)
BufferedReader br =new BufferedReader(new FileReader(src));
//操作 行读取
String line=null;
while(null!=(line=br.readLine())){
System.out.println(line);
}
//释放资源
br.close();

4、转换处理流: InputStreamReader

转换流:将字节流转为字符流 处理乱码(编码集、解码集)。

//读取文件
File src =new File("test.txt");
//转换流
BufferedReader br =new BufferedReader(
new InputStreamReader(
new BufferedInputStream(
new FileInputStream(
src
) ),"utf-8"
)
);
//行读取
String msg =null;
while(null!=(msg =br.readLine())){
System.out.println(msg);
}
br.close();

5、字节数组节点类: ByteArrayInputStream

操作的节点为字节数组,数组在jvm 内存中,由垃圾回收机制管理,不需要手动关闭

//1、创建源
byte[] src ="io 学习入门".getBytes();
//2、选择流
InputStream is = new ByteArrayInputStream(src);
//3、操作 与文件一致
byte[] flush =new byte[10];
int len =0;
while(-1!=(len =is.read(flush))){
System.out.println(new String(flush,0,len));
}
//4、释放
is.close();

6、数据处理流:DataInputStream

可以处理基本类型+String,保留数据的类型。前提是读取顺序与写出顺序一致,否则读取数据不正确

/**
* 数据+类型 输出到文件
* @param destPath
* @throws IOException
*/
public static void write(String destPath) throws IOException{
int point=2;
long num=100L;
String str="数据类型";
//创建源
File dest=new File(destPath);
//选择流
DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
//操作 写出的顺序 为读取作准备
dos.writeInt(point);
dos.writeLong(num);
dos.writeUTF(str);
dos.flush();
//释放资源
dos.close();
}

7、对象处理流(反序列化):ObjectInputStream

/**
* 反序列化:
* 1、先写入再读取
* 2、读取对象需要知道具体类型,依次读取
* 注意:
* 1)不是所有的对象都可以序列化 Serializable
* 2)不是所有的属性都需要序列化 transient
*/
public static void read(String srcPath) throws FileNotFoundException, IOException, ClassNotFoundException{
//创建源
File src=new File(srcPath);
//选择流 OjbectInputStream
ObjectInputStream dis=new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(src)
)
);
//操作 读取的顺序与写出的顺序一致 必须存在才能读取
Object obj=dis.readObject();
if(obj instanceof Employee){
Employee emp=(Employee)obj;
System.out.println(emp.getName());
System.out.println(emp.getSalary());
}
obj=dis.readObject();
int[]arr=(int[])obj;
System.out.println(Arrays.toString(arr));
//释放资源
dis.close();
}

Java中的IO流-输入流就介绍到这里了,下次再说输出流。

乐字节原创,更多Java技术干货持续更新,欢迎关注。