美文网首页
Java-I/O流

Java-I/O流

作者: 小的橘子 | 来源:发表于2019-03-04 16:31 被阅读0次

总结

I/O流分类

  • 按照操作单元划分,字节I/O系统字符I/O系统
  • 按照流的流向分,可以将流分为输入流输出流

所有超类

  • java.io.InputStream
  • java.io.OutputStream
  • java.io.Reader
  • java.io.Writer

常用流类结构

按操作方式分类结构图:
按操作对象分类结构图

流特点

带Buffered的性能高,使用了缓存,将数据保存在内存中,一定量时再进行写入磁盘,提高了效率。

字节流 InputStream OutputStream

FileInputStream/FileOutputStream

public static void writeFile() {
    FileOutputStream fos = null;
    char a = 'A';
    char b = '中';
    String s = "Hello 中国";
    try {
        fos = new FileOutputStream("data1.txt");
        fos.write(a);
        // fos.write(b); // 中文占两个字节,虽然write接收int,但实际只输出低位一个字节,故该句会引起乱码
        fos.write(String.valueOf(b).getBytes());
        fos.write(s.getBytes()); // Project Encoding为UTF-8,故以utf-8编码解码字节 data1.txt根据字节特征识别为utf-8格式,故不会乱码
        fos.write(s.getBytes("gbk")); // 以gbk编码解码字节,由于以包含utf-8字节,再放入gbk编码字节,data1.txt无法识别编码,故会乱码
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public static void readFile() {
    FileInputStream fis = null;
    try {
        // 可以指定全路径,也可以相对路径(相对于工程根目录)
        fis = new FileInputStream("data.txt");
        byte[] bytes = new byte[10];
        // 读取出来的就是硬盘上该文件内容的字节序列
        int v = fis.read(bytes);
        while (v != -1) {
            // 输出到控制台,默认以ANSI编码解析,如果Studio控制台指定了编码格式,则以对应编码格式解析
            System.out.write(bytes, 0, v);
            v = fis.read(bytes);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

DataInputStream/DataOutputSrream

public static void binaryRead() {
    FileInputStream fis = null;
    DataInputStream dis = null;
    try {
        fis = new FileInputStream("data.txt");
        dis = new DataInputStream(fis);
        // 读取顺序必须完全一致
        byte a = dis.readByte();
        boolean flag = dis.readBoolean();
        int b = dis.readInt();
        float f = dis.readFloat();
        String str = dis.readUTF(); // 通过UTF-8编码解析基础输入流的字节
        System.out.println(a);
        System.out.println(flag);
        System.out.println(b);
        System.out.println(f);
        System.out.println(str);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

public static void binaryWrite() {
    byte a = 1;
    boolean flag = true;
    int b = 10;
    float f = 30.12f;
    String str = "Hello 中国";
    FileOutputStream fos = null;
    DataOutputStream dos = null;
    try {
        fos = new FileOutputStream("data.txt");
        dos = new DataOutputStream(fos);
        dos.writeByte(a);
        dos.writeBoolean(flag);
        dos.writeInt(b);
        dos.writeFloat(f);
        dos.writeUTF(str); // 使用 UTF-8编码将一个字符串写入基础输出流
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                // 只需关外层流
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

PipedInputStream/PipedOutputStream

public class Test {
    public static void main(String[] args) {
        /**
         * 流程
         * 1 建立输入输出流
         * 2 绑定输入输出流
         * 3 向缓冲区写数据
         * 4 读取缓冲区数据
         */
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream();
        try {
            pos.connect(pis); // 接通输入和输出形成管道
            Producer producer = new Producer(pos);
            Consumer consumer = new Consumer(pis);
            producer.start();
            consumer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
/**
 * 生产者线程
 */
class Producer extends Thread {
    // 输出流
    private PipedOutputStream out = new PipedOutputStream();

    // 构造方法
    public Producer(PipedOutputStream out) {
        this.out = out;
    }

    @Override
    public void run() {
        writeMessage();
    }

    private void writeMessage() {
        StringBuilder sb = new StringBuilder("Hello World!!!");
        try {
            out.write(sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

/**
 * 消费线程
 */
class Consumer extends Thread {
    //输入流, 默认缓冲区大小为1024
    private PipedInputStream in = new PipedInputStream();

    //构造方法
    public Consumer(PipedInputStream in) {
        this.in = in;
    }

    @Override
    public void run() {
        readMessage();
    }

    private void readMessage() {
        byte[] buf = new byte[1024];
        try {
            int len = in.read(buf);
            System.out.println("缓冲区的内容为: " + new String(buf, 0, len));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

字符流

InputStreamReader/OutputStreamWriter

private static void charRead() {
    FileInputStream fis = null;
    InputStreamReader isr = null;
    try {
        fis = new FileInputStream("data.txt");
        // 默认使用系统设定编码,也可指定编码,eg:使用utf-8编码解析,如果写入和读取编码不一致,很可能乱码
        isr = new InputStreamReader(fis, "UTF-8");
        char[] chars = new char[10];
        int count = isr.read(chars);
        while (count != -1) {
            System.out.print(new String(chars, 0, count));
            count = isr.read(chars);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

private static void charWrite() {
    FileOutputStream fos = null;
    OutputStreamWriter osw = null;
    try {
        fos = new FileOutputStream("data.txt");
        osw = new OutputStreamWriter(fos);
        osw.write("Hello 中");
        osw.write('国');
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (osw != null) {
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

BufferedReader/BufferedWriter

public static void bufferReadWrite() {
    // 读取用户从控制台的输入,并写入文件,直到用户输入了ok为止
    String input = null;
    BufferedReader br = null;
    BufferedWriter bw = null;
    String filePath = "data.txt";
    br = new BufferedReader(new InputStreamReader(System.in));
    try {
        bw = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(filePath)));
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    do {
        System.out.print("请输入一行文字:");
        try {
            input = br.readLine();
            bw.write(input);
            bw.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } while (!input.equals("ok"));

    try {
        br.close();
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // 从文件中读取内容并显示
    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(
                filePath)));
        input = br.readLine();
        while (input != null) {
            System.out.println(input);
            input = br.readLine();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

System.out/System.in PrintStream/InputStream

out就是PrintStream类,其print方法都是通过BufferedWriter实现的。

相关文章

  • Java-I/O流

    总结 I/O流分类 按照操作单元划分,字节I/O系统和字符I/O系统。 按照流的流向分,可以将流分为输入流和输出流...

  • Java-I/O学习(5)

    Java-I/O学习(5) ByteArrayInputStream ByteArrayInputStream类可...

  • Java-I/O学习(6)

    Java-I/O学习(6) DataInputStream DataInputStream可以让你从InputSt...

  • Java-I/O学习(3)

    Java-I/O学习(3) RandomAccessFile 构造函数 构造方法具体描述RandomAccessF...

  • Java-I/O学习(7)

    Java-I/O学习(7) Reader 在Java API中,Java Reader类(java.io.Read...

  • Java-I/O学习(2)

    Java-I/O学习(2) InputStream InputStream是Java IO中所有输入流的基类 方法...

  • Java-I/O系统

    对程序语言的设计者来说,创建一个好的输出/输入(I/O)系统是一项艰难的任务。——《Thinking in Jav...

  • Java-I/O学习(4)

    Java-I/O学习(4) File Java IO 的File类可以帮助你访问底层的文件系统,使用File类你可...

  • Java-I/O学习(1)

    Java-I/O学习(1) Java IO是java中的相关API,主要目的为读数据与写数据(input 和 ou...

  • java基础-I/O流

    知识点 I/O流的基本知识 I/O流中常用流的关系 I/O流之FileInputSrteam,FileOutput...

网友评论

      本文标题:Java-I/O流

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