(每日持续更新)jdk api之DataInputStream基础、应用、实战
博主18年的互联网软件开发经验,从一名程序员小白逐步成为了一名架构师,我想通过平台将经验分享给大家,因此博主每天会在各个大牛网站点赞量超高的博客等寻找该技术栈的资料结合自己的经验,晚上进行用心精简、整理、总结、定稿,每天都会整理到12点,为了就是能让大家能够真正了解该技术栈的真正原理,最终从程序员成为一名真正的架构师,写的不一定是全站做好的,但是是全站最用心的~。
以后我会推出一些列的文章,每天都会更新,每天进步一点点,发布顺序【java的api基础、应用、实战】->【java开源技术栈及源码分析】->【java开源技术栈整合】->【java低代码开发平台的建设】
一、java.io
1.15 DataInputStream
DataInputStream 类是 DataInput 接口的实现类,它提供了一组用于从输入流中读取基本数据类型的方法。DataInputStream 允许应用程序以原始数据类型的形式从输入流中读取数据。
以下是关于 DataInputStream 类的主要信息:
DataInputStream 类介绍:
-
包路径:
java.io -
父类:
FilterInputStream -
实现接口:
DataInput
DataInputStream 类所有字段:
DataInputStream 类没有定义任何字段。
DataInputStream 类构造方法:
-
DataInputStream(InputStream in):-
构造一个新的
DataInputStream,给定底层输入流in。
-
DataInputStream 类方法摘要:
以下是一些主要的方法,继承自 DataInput 接口:
-
int read(byte[] b):-
从输入流中读取一些字节数,并将它们存储到缓冲区数组
b中。
-
-
boolean readBoolean():-
从输入流中读取一个布尔值。
-
-
byte readByte():-
从输入流中读取一个字节。
-
-
char readChar():-
从输入流中读取一个字符。
-
-
double readDouble():-
从输入流中读取一个双精度浮点数。
-
-
float readFloat():-
从输入流中读取一个单精度浮点数。
-
-
void readFully(byte[] b):-
从输入流中读取字节数组,并将其存储到指定的字节数组
b中。
-
-
void readFully(byte[] b, int off, int len):-
从输入流中读取指定长度的字节数组,并将其存储到指定的字节数组
b中,从数组的偏移位置off开始。
-
-
int readInt():-
从输入流中读取一个整数。
-
-
String readLine():-
从输入流中读取一行文本。
-
-
long readLong():-
从输入流中读取一个长整数。
-
-
short readShort():-
从输入流中读取一个短整数。
-
-
int readUnsignedByte():-
从输入流中读取一个无符号字节,将其作为
int返回。
-
-
int readUnsignedShort():-
从输入流中读取一个无符号短整数,将其作为
int返回。
-
-
String readUTF():-
从输入流中读取以修饰符长度前缀编码的字符串。
-
简单使用例子:
以下是一个简单的例子,演示了如何使用 DataInputStream 从二进制文件中读取基本数据类型的信息:
javaCopy code
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
?
public class DataInputStreamExample {
?
? public static void main(String[] args) {
? ? ? try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("data.dat"))) {
? ? ? ? ? // 从输入流中读取不同类型的数据
? ? ? ? ? boolean boolValue = dataInputStream.readBoolean();
? ? ? ? ? byte byteValue = dataInputStream.readByte();
? ? ? ? ? short shortValue = dataInputStream.readShort();
? ? ? ? ? int intValue = dataInputStream.readInt();
? ? ? ? ? long longValue = dataInputStream.readLong();
? ? ? ? ? float floatValue = dataInputStream.readFloat();
? ? ? ? ? double doubleValue = dataInputStream.readDouble();
? ? ? ? ? String stringValue = dataInputStream.readUTF();
?
? ? ? ? ? // 打印读取的数据
? ? ? ? ? System.out.println("Boolean: " + boolValue);
? ? ? ? ? System.out.println("Byte: " + byteValue);
? ? ? ? ? System.out.println("Short: " + shortValue);
? ? ? ? ? System.out.println("Int: " + intValue);
? ? ? ? ? System.out.println("Long: " + longValue);
? ? ? ? ? System.out.println("Float: " + floatValue);
? ? ? ? ? System.out.println("Double: " + doubleValue);
? ? ? ? ? System.out.println("String: " + stringValue);
?
? ? ? } catch (IOException e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
? }
}
在这个例子中,我们使用 DataInputStream 包装一个 FileInputStream,然后通过 DataInput 接口的方法逐个读取不同类型的数据。这个例子假设存在一个名为 "data.dat" 的二进制数据文件。
使用场景
DataInputStream 主要用于从输入流中读取基本数据类型的二进制数据。以下是一些使用场景:
-
文件解析: 当需要从二进制文件中读取原始数据类型的信息时,例如读取自定义格式的配置文件或数据文件。
javaCopy code try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("config.dat"))) { ? int intValue = dataInputStream.readInt(); ? float floatValue = dataInputStream.readFloat(); ? // 处理读取的数据 } catch (IOException e) { ? e.printStackTrace(); } -
网络通信: 在网络通信中,可以使用
DataInputStream从输入流中读取二进制数据,以解析从网络接收到的消息。javaCopy code try (DataInputStream dataInputStream = new DataInputStream(socket.getInputStream())) { ? int messageLength = dataInputStream.readInt(); ? byte[] messageData = new byte[messageLength]; ? dataInputStream.readFully(messageData); ? // 处理接收到的消息数据 } catch (IOException e) { ? e.printStackTrace(); } -
序列化: 在使用自定义的序列化格式时,可以使用
DataInputStream读取对象的二进制表示。javaCopy code try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("serialized_data.dat"))) { ? int intValue = dataInputStream.readInt(); ? String stringValue = dataInputStream.readUTF(); ? // 从二进制数据中重新构造对象 ? MyObject reconstructedObject = new MyObject(intValue, stringValue); } catch (IOException e) { ? e.printStackTrace(); } -
数据导入: 当需要从二进制数据源中导入原始数据类型时,例如从数据库中读取二进制数据列。
javaCopy code try (DataInputStream dataInputStream = new DataInputStream(databaseInputStream)) { ? int intValue = dataInputStream.readInt(); ? float floatValue = dataInputStream.readFloat(); ? // 处理读取的数据 } catch (IOException e) { ? e.printStackTrace(); }
总体而言,DataInputStream 的主要应用场景是在需要读取原始数据类型的二进制数据时,提供了方便的方法来逐个读取各种基本数据类型。注意,对于对象的序列化和反序列化,通常会使用 ObjectInputStream,它实现了 ObjectInput 接口。
实战例子
在实际项目中,使用 DataInputStream 的复杂场景通常涉及更复杂的数据结构和业务逻辑。下面是一个简化的示例,假设有一个二进制文件 "order_data.dat",其中包含了订单信息,包括订单编号、客户姓名、商品列表等。
首先,我们定义一个 Order 类来表示订单信息:
javaCopy code
import java.io.Serializable;
import java.util.List;
?
public class Order implements Serializable {
? private static final long serialVersionUID = 1L;
?
? private int orderId;
? private String customerName;
? private List<String> products;
?
? // 省略构造方法和其他方法
?
? // Getter 和 Setter 方法
? // ...
}
然后,我们可以编写一个订单数据读取器类来从二进制文件中读取订单信息:
javaCopy code
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
?
public class OrderDataReader {
?
? public static void main(String[] args) {
? ? ? try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("order_data.dat"))) {
? ? ? ? ? List<Order> orders = new ArrayList<>();
?
? ? ? ? ? // 读取每个订单的信息
? ? ? ? ? while (dataInputStream.available() > 0) {
? ? ? ? ? ? ? int orderId = dataInputStream.readInt();
? ? ? ? ? ? ? String customerName = dataInputStream.readUTF();
? ? ? ? ? ? ? int productCount = dataInputStream.readInt();
?
? ? ? ? ? ? ? List<String> products = new ArrayList<>();
? ? ? ? ? ? ? for (int i = 0; i < productCount; i++) {
? ? ? ? ? ? ? ? ? String productName = dataInputStream.readUTF();
? ? ? ? ? ? ? ? ? products.add(productName);
? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? // 创建 Order 对象并添加到列表中
? ? ? ? ? ? ? Order order = new Order(orderId, customerName, products);
? ? ? ? ? ? ? orders.add(order);
? ? ? ? ? }
?
? ? ? ? ? // 处理读取的订单信息,例如存储到数据库或进行其他业务操作
? ? ? ? ? for (Order order : orders) {
? ? ? ? ? ? ? System.out.println("Order ID: " + order.getOrderId());
? ? ? ? ? ? ? System.out.println("Customer Name: " + order.getCustomerName());
? ? ? ? ? ? ? System.out.println("Products: " + order.getProducts());
? ? ? ? ? ? ? System.out.println("-----------");
? ? ? ? ? }
?
? ? ? } catch (IOException e) {
? ? ? ? ? e.printStackTrace();
? ? ? }
? }
}
在这个示例中,我们按照一定的格式将订单信息写入到二进制文件 "order_data.dat" 中,然后使用 DataInputStream 逐个读取每个订单的信息。该示例包含了订单编号、客户姓名和商品列表,实际项目中可能还需要考虑更多的信息和复杂的业务逻辑。
请注意,这只是一个简化的示例,实际项目中可能需要更多的数据验证、错误处理和完整的业务逻辑。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!