Java DataInputStream 类
java datainputstream 类
数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 java 数据类型。
1. datainputstream 创建方式
下面的构造方法用来创建数据输入流对象。
datainputstream dis = new datainputstream(inputstream in);
2. datainputstream 操作方法
| 序号 | 方法描述 |
|---|---|
| 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 从输入流中读取下一文本行。 |
3. datainputstream 范例
下面的例子演示了datainputstream和dataoutputstream的使用,该例从文本文件test.txt中读取5行,并转换成大写字母,最后保存在另一个文件test1.txt中。
test.txt 文件内容如下:
yapf1 yapf2 yapf3 yapf4 yapf5
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();
}
}
以上实例编译运行结果如下:
yapf1 yapf2 yapf3 yapf4 yapf5

java 文件读写流
