java IO流之DataInputStream DataOutputStream 数据流
数据流:用来操作基本数据类型(byte short int long double float char boolean
)和字符串(String
)
DataInputStream
将文件中存储的基本数据类型和字符串写入内存的变量中。数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。
构造方法摘要
- DataInputStream(InputStream in)
- 使用指定的底层 InputStream 创建一个 DataInputStream。
方法
Modifier and Type | Method | Description |
---|---|---|
int | read(byte[] b) | 从包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中。 |
int | read(byte[] b, int off, int len) | 从包含的输入流中将最多 len 个字节读入一个 byte 数组中。 |
void | readFully(byte[] b) | 从输入流中读取一些字节,并将它们存储在缓冲区数组 b 中。 |
void | readFully(byte[] b, int off, int len) | 从输入流中读取 len 个字节。 |
String | readUTF() | 读入一个已使用 UTF-8 修改版格式编码的字符串。 |
static String | readUTF(DataInput in) | 从流 in 中读取用 UTF-8 修改版格式编码的 Unicode 字符格式的字符串;然后以 String 形式返回此字符串。 |
String | readLine() | 从输入流中读取下一文本行。 |
int | readUnsignedByte() | 读取一个输入字节,将它左侧补零 (zero-extend) 转变为 int 类型,并返回结果,所以结果的范围是 0 到 255。 |
int | readUnsignedShort() | 读取两个输入字节,并返回 0 到 65535 范围内的一个 int 值。 |
int | skipBytes(int n) | 试图在输入流中跳过数据的 n 个字节,并丢弃跳过的字节。 |
boolean | readBoolean() | 读取一个输入字节,如果该字节不是零,则返回 true,如果是零,则返回 false。 |
byte | readByte() | 读取并返回一个输入字节。该字节被看作是 -128 到 127(包含)范围内的一个有符号值。 |
char | readChar() | 读取两个输入字节并返回一个 char 值。 |
double | readDouble() | 读取八个输入字节并返回一个 double 值。 |
float | readFloat() | 读取四个输入字节并返回一个 float 值。 |
int | readInt() | 读取四个输入字节并返回一个 int 值。 |
long | readLong() | 读取八个输入字节并返回一个 long 值。 |
short | readShort() | 读取两个输入字节并返回一个 short 值。 |
- 从类 java.io.FilterInputStream 继承的方法
available, close, mark, markSupported, read, reset, skip
- 从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
DataOutputStream
将内存中的基本数据类型和字符串的变量写出文件中。数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。
构造方法
- DataOutputStream(OutputStream out)
- 创建一个新的数据输出流,将数据写入指定基础输出流。
方法
Modifier and Type | Method | Description |
---|---|---|
void | flush() | 清空此数据输出流。 |
int | size() | 返回计数器 written 的当前值,即到目前为止写入此数据输出流的字节数。 |
void | write(byte[] b) | 将 b.length 个字节写入此输出流。 |
void | write(byte[] b, int off, int len) | 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入基础输出流。 |
void | write(int b) | 将指定字节(参数 b 的八个低位)写入基础输出流。 |
void | writeBoolean(boolean v) | 将一个 boolean 值以 1-byte 值形式写入基础输出流。 |
void | writeByte(int v) | 将一个 byte 值以 1-byte 值形式写出到基础输出流中。 |
void | writeBytes(String s) | 将字符串按字节顺序写出到基础输出流中。 |
void | writeChar(int v) | 将一个 char 值以 2-byte 值形式写入基础输出流中,先写入高字节。 |
void | writeChars(String s) | 将字符串按字符顺序写入基础输出流。 |
void | writeDouble(double v) | 使用 Double 类中的 doubleToLongBits 方法将 double 参数转换为一个 long 值,然后将该 long 值以 8-byte 值形式写入基础输出流中,先写入高字节。 |
void | writeFloat(float v) | 使用 Float 类中的 floatToIntBits 方法将 float 参数转换为一个 int 值,然后将该 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。 |
void | writeInt(int v) | 将一个 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。 |
void | writeLong(long v) | 将一个 long 值以 8-byte 值形式写入基础输出流中,先写入高字节。 |
void | writeShort(int v) | 将一个 short 值以 2-byte 值形式写入基础输出流中,先写入高字节。 |
void | writeUTF(String str) | 以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。 |
void | close() | 关闭此输出流并释放与此流有关的所有系统资源。 |
- 从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
实例化
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-USjWcvEf-1629012360395)(img\Data数据流.png)]
写入示例
public class DosT {
public static void main(String[] args){
/*File f = new File("IOStream/src/testfile/test.txt);
FileOutputStream fos = new FileOutputStream(f);
DataOutputStream dos = new DataOutputStream(fos);*/
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream(new File("IOStream/src/testfile/test.txt")));
//向外将变量写到文件中去:
dos.writeUTF("hello");
dos.writeInt(8);
dos.writeDouble(2.6);
dos.writeBoolean(false);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("找不到文件");
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流:
try {
if (dos != null) {
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
读出示例
public class DinT {
public static void main(String[] args){
//DataInputStream:将文件中存储的基本数据类型和字符串入内存的变量中
try (
DataInputStream dis = new DataInputStream(new FileInputStream(new File("IOStream/src/testfile/test.txt")));
){
//将文件中内容读取到程序中来:注意读出顺序要与写入一致
System.out.println(dis.readUTF());
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
}catch(Exception e){
e.printStackTrace();
}
}
}