2021-09-21 IO流(操作基本数据类型的流对象DataS

2021-10-16  本文已影响0人  Denholm

IO包中的其他类

操作基本数据类型

DataInputStream和DataOutputStream

操作字符数组

CharArrayReader和CharArrayWriter

操作字符串

StringReader和StringWriter
import java.io.*;

public class DataStreamDemo {

    public static void main(String[] args) throws Exception {
        writeFile();
        readFile();

        OutputStreamWriter osw1 = new OutputStreamWriter(
                new FileOutputStream("E:\\utf.txt"), "utf-8");
        OutputStreamWriter osw2 = new OutputStreamWriter(
                new FileOutputStream("E:\\gbk.txt"), "gbk");
        osw1.write("你好"); // utf-8  6byte
        osw2.write("你好"); // gbk    4byte
        osw1.close();
        osw2.close();
    }

    public static void readFile() throws Exception {
        DataInputStream dis = new DataInputStream(
                new FileInputStream("E:\\data.txt"));
        int i = dis.readInt();
        boolean b = dis.readBoolean();
        double d = dis.readDouble();
        String utf = dis.readUTF();
        System.out.println(i);
        System.out.println(b);
        System.out.println(d);
        System.out.println(utf);
        dis.close();
    }

    public static void writeFile() throws Exception {
        DataOutputStream dos = new DataOutputStream(
                new FileOutputStream("E:\\data.txt"));
        dos.writeInt(47);
        dos.writeBoolean(false);
        dos.writeDouble(99.99);
        dos.writeUTF("你好");
        dos.close();
    }

}
上一篇 下一篇

猜你喜欢

热点阅读