1. DataOutputStream(字节数据输出流)
允许把数据和其类型一并写出去。
构造器 | 说明 |
---|---|
public DataOutputStream(OutputStream outputStream) | 创建新数据输出流包装基础的字节输出流 |
方法 | 说明 |
---|---|
public final void writeByte(int v) throws IOException | 将byte类型的数据写入基础的字节输出流 |
public final void writeInt(int v) throws IOException | 将int类型的数据写入基础的字节输出流 |
public final void writeDouble(Double v) throws IOException | 将Double类型的数据写入基础的字节输出流 |
public final void writeUTF(String str) throws IOException | 将字符串数据以UTF-8编码成字节写入基础的字节输出流 |
public void write(int/byte[]/byte[]的一部分) | 支持写字节数据出去 |
public final void writeBoolean(boolean v) throws IOException | 将boolean类型的数据写入基础的字节输出流 |
public class DataOutputStreamTest {
private static final String SAVE_PATH = "D:\\repo\\javasepro\\helloworld-app\\src\\com\\ming\\File_\\static\\DataOutputStreamTest.txt";
public static void main(String[] args) {
// 目标:掌握字节数据输出流
// 1. public DataOutputStream(OutputStream outputStream) 创建新数据输出流包装基础的字节输出流
// 1. public final void writeByte(int v) throws IOException 将byte类型的数据写入基础的字节输出流
// 2. public final void writeInt(int v) throws IOException 将int类型的数据写入基础的字节输出流
// 3. public final void writeDouble(Double v) throws IOException 将Double类型的数据写入基础的字节输出流
// 4. public final void writeBoolean(boolean v) throws IOException 将boolean类型的数据写入基础的字节输出流
// 5. public final void writeUTF(String str) throws IOException 将字符串数据以UTF-8编码成字节写入基础的字节输出流
// 6. public void write(int/byte[]/byte[]的一部分) 支持写字节数据出去
try (
// 1. 创建一个数据输出流包装低级的字节输出流
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(SAVE_PATH));
) {
// 1. public final void writeByte(int v) throws IOException 将byte类型的数据写入基础的字节输出流
dataOutputStream.writeByte(97);
// 2. public final void writeInt(int v) throws IOException 将int类型的数据写入基础的字节输出流
dataOutputStream.writeInt(97);
// 3. public final void writeDouble(Double v) throws IOException 将Double类型的数据写入基础的字节输出流
dataOutputStream.writeDouble(97.5);
// 4. public final void writeBoolean(boolean v) throws IOException 将boolean类型的数据写入基础的字节输出流
dataOutputStream.writeBoolean(true);
// 5. public final void writeUTF(String str) throws IOException 将字符串数据以UTF-8编码成字节写入基础的字节输出流
dataOutputStream.writeUTF("床前明月光, 疑是地上霜");
} catch (Exception e) {
e.printStackTrace();
}
}
}
2. DataInputStream(字符数据输入流)
用于读取数据输出流写出的数据
注意:读取数据的顺序要跟写入数据的顺序一致,否则会报错!!!
构造器 | 说明 |
---|---|
public DataInputStream(InputStream inputStream) | 创建新数据输出流包装基础的字节输入流 |
方法 | 说明 |
---|---|
public final void readByte() throws IOException | 读取字节数据返回 |
public final void readInt() throws IOException | 读取int数据返回 |
public final void readDouble() throws IOException | 读取Double数据返回 |
public final void readBoolean() throws IOException | 读取Boolean数据返回 |
public final void readUTF(String str) throws IOException | 读取字符串(UTF-8)数据返回 |
public void read(byte[]) | 支持读取字节数据出去 |
public class DataInputStreamTest {
private static final String SAVE_PATH = "D:\\repo\\javasepro\\helloworld-app\\src\\com\\ming\\File_\\static\\DataOutputStreamTest.txt";
public static void main(String[] args) {
// 目标:掌握字节数据输入流
// 1. public DataInputStream(InputStream inputStream) 创建新数据输出流包装基础的字节输入流
// 1. public final void readByte() throws IOException 读取字节数据返回
// 2. public final void readInt() throws IOException 读取int数据返回
// 3. public final void readDouble() throws IOException 读取Double数据返回
// 4. public final void readBoolean() throws IOException 读取Boolean数据返回
// 5. public final void readUTF(String str) throws IOException 读取字符串(UTF-8)数据返回
// 6. public void read(byte[]) 支持读取字节数据出去
try (
// 1. public DataInputStream(InputStream inputStream) 创建新数据输出流包装基础的字节输入流
DataInputStream dataInputStream= new DataInputStream(new FileInputStream(SAVE_PATH))
) {
// 1. public final void readByte() throws IOException 读取字节数据返回
System.out.println(dataInputStream.readByte());
// 2. public final void readInt() throws IOException 读取int数据返回
System.out.println(dataInputStream.readInt());
// 3. public final void readDouble() throws IOException 读取Double数据返回
System.out.println(dataInputStream.readDouble());
// 4. public final void readBoolean() throws IOException 读取Boolean数据返回
System.out.println(dataInputStream.readBoolean());
// 5. public final void readUTF(String str) throws IOException 读取字符串(UTF-8)数据返回
System.out.println(dataInputStream.readUTF());
} catch (Exception e) {
e.printStackTrace();
}
}
}