20230622 java.io.DataOutput

介绍

  • java.io.DataOutput
  • public interface DataOutput
  • 定义了用于以二进制格式写数组、字符、 boolean 值和字符串的方法

API

写字节

  • write
    • void write(int b) throws IOException;
    • void write(byte b[]) throws IOException;
    • void write(byte b[], int off, int len) throws IOException;

写基本数据类型

  • writeBoolean
  • writeByte
  • writeShort
  • writeChar
  • writeInt
  • writeLong
  • writeFloat
  • writeDouble

写 String

  • writeBytes
  • writeChars
  • writeUTF
    • 使用修订版的 Unicode 转换格式,用于 Java 虚拟机
    • 其他场合都应该使用 writeChars

示例代码

DataOutputStream out = new DataOutputStream(new FileOutputStream("data1.dat"));
out.writeInt(1);
out.writeChars("data");
out.close();

DataInputStream in = new DataInputStream(new FileInputStream("data1.dat"));
System.out.println(in.readInt());
final char c1 = in.readChar();
final char c2 = in.readChar();
final char c3 = in.readChar();
final char c4 = in.readChar();
in.close();

System.out.println("" + c1 + c2 + c3 + c4);
posted @ 2023-08-23 09:57  流星<。)#)))≦  阅读(9)  评论(0编辑  收藏  举报