美文网首页
2021-09-21 IO流(操作基本数据类型的流对象DataS

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

作者: Denholm | 来源:发表于2021-10-16 14:56 被阅读0次

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();
    }

}

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/prybgltx.html