Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
页面加载耗时 0.00 毫秒·物理内存 129.0MB ·虚拟内存 1437.5MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。
下面的构造方法用来创建数据输入流对象。
DataInputStream dis = new DataInputStream(InputStream in);
| 序号 | 方法描述 |
|---|---|
| 1 | public final int read(byte[] r, int off, int len)throws IOException从所包含的输入流中将 len 个字节读入一个字节数组中。如果len为-1,则返回已读字节数。 |
| 2 | Public final int read(byte [] b)throws IOException从所包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中。 |
| 3 | public final Boolean readBooolean()throws IOException从所包含的输入流中读取一个布尔型数据。 |
| 4 | public final byte readByte()throws IOException从所包含的输入流中读取一个字节数据。 |
| 5 | public final short readShort()throws IOException从所包含的输入流中读取一个短整型数据。 |
| 6 | public final Int readInt()throws IOException从所包含的输入流中读取一个整型数据。 |
| 7 | public String readLine() throws IOException从输入流中读取下一文本行。 |
下面的例子演示了DataInputStream和DataOutputStream的使用,该例从文本文件test.txt中读取5行,并转换成大写字母,最后保存在另一个文件test1.txt中。
test.txt 文件内容如下:
aizws1 aizws2 aizws3 aizws4 aizws5
import java.io.*;
public class Test{
public static void main(String args[])throws IOException{
DataInputStream in = new DataInputStream(new FileInputStream("test.txt"));
DataOutputStream out = new DataOutputStream(new FileOutputStream("test1.txt"));
BufferedReader d = new BufferedReader(new InputStreamReader(in));
String count;
while((count = d.readLine()) != null){
String u = count.toUpperCase();
System.out.println(u);
out.writeBytes(u + " ,");
}
d.close();
out.close();
}
}
以上实例编译运行结果如下:
CODEBAOKU1 CODEBAOKU2 CODEBAOKU3 CODEBAOKU4 CODEBAOKU5
数据输出流允许应用程序以与机器无关方式将Java基本数据类型写到底层输出流。DataOutputStream 创建方式:下面的构造方法用来创建数据输出流对象:DataOutputStream out = new DataOutputStream(OutputStream out);DataOutputStream 操作方法:创建对象成功后,可以参照以下列表给出的方法,对流进行写操作或者其他操作。