与字符流基本相同
package cn.hxh.io.other;
import java.io.*;
public class DataDemo01 {
public static void main(String[] args) throws IOException {
write("D:/aa/a.txt");
read("D:/aa/a.txt");
}
public static void read(String destPath) throws IOException {
File dest = new File(destPath);
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(dest)));
int i = dis.readInt();
long l = dis.readLong();
String s = dis.readUTF();
System.out.println(i + " " + l + " " + s);
dis.close();
}
public static void write(String destPath) throws IOException {
int i = 1;
long l = 100;
String s = "字符流写入测试";
File dest = new File(destPath);
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
dos.writeInt(i);
dos.writeLong(l);
dos.writeUTF(s);
dos.flush();
dos.close();
}
}
与字符流基本相同
package cn.hxh.io.other;
import java.io.*;
public class DataDemo02 {
public static void main(String[] args) throws IOException {
read(write());
}
public static void read(byte src[]) throws IOException {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(src)));
int i = dis.readInt();
long l = dis.readLong();
String s = dis.readUTF();
System.out.println(i + " " + l + " " + s);
dis.close();
}
public static byte[] write() throws IOException {
int i = 1;
long l = 100;
String s = "字符流写入测试";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(bos));
dos.writeInt(i);
dos.writeLong(l);
dos.writeUTF(s);
dos.flush();
dos.close();
return bos.toByteArray();
}
}