IO流

对文件进行操作:

写出到文件:

FileWriter(字符输出流),FileOutputStream(字节输出流)

        String str = "中国人";
        FileWriter fw = new FileWriter("1.txt");
        fw.write(str);
        fw.close();
        // 写出
        String str = "中国人";
        FileOutputStream fos = new FileOutputStream("2.txt");
        fos.write(str.getBytes("gbk"));
        fos.close();

从文件读入:

FileReader(字符输入流),FileInputStream(字节输入流)

        FileReader fr = new FileReader("1.txt");
        char[] buf = new char[1024];
        int len = fr.read(buf);
        String myStr = new String(buf, 0, len);
        System.out.println(myStr);
        // 读入
        FileInputStream fr = new FileInputStream("1.txt");
        int bestAvailable = fr.available();
        if (bestAvailable > 0) {
            byte[] buf = new byte[bestAvailable];
            int c;
            while ((c = fr.read(buf, 0, buf.length)) != -1) {
                String string = new String(buf, 0, c);
                System.out.println(string);
            }
        }
        fr.close();

对管道进行操作:

PipedWriter(字符输出流),PipedOutStream(字节输出流),PipedReader(字符输入流),PipedInputStream(字节输入流)
PipedInputStream的一个实例要和PipedOutputStream的一个实例共同使用,共同完成管道的读取写入操作。主要用于线程操作。

字节/字符数组:

CharArrayWriter,ByteArrayOutputStream,CharArrayReader,ByteArrayInputStream,是在内存中开辟了一个字节或字符数组。

Buffered缓冲流:

BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter是带缓冲区的处理流,缓冲区的作用的主要目的是:避免每次和硬盘打交道,提高数据访问的效率。

文件拷贝:

package com.jef.io.blog.file.copy;

import java.io.*;

/**
 * @author Jef
 */
public class FileCopy {

    public static void copyFile(File fromFile, File toFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fromFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(toFile));
        int in;
        while ((in = bis.read()) != -1) {
            bos.write(in);
            // 清空缓冲区
            bos.flush();
        }
        bis.close();
        bos.close();
    }

    public static void copyFileTwo(File fromFile, File toFile) throws IOException {
        FileReader fr = new FileReader(fromFile);
        FileWriter fw = new FileWriter(toFile);
        char[] buffer = new char[8 * 1024];
        int c;
        while ((c = fr.read(buffer, 0, buffer.length)) != -1) {
            String string = new String(buffer, 0, c);
            System.out.print(string);
            fw.write(buffer, 0, c);
            fw.flush();
        }
        fr.close();
        fw.close();
    }

    public static void copyFileThree(File fromFile, File toFile) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fromFile));
        BufferedWriter bw = new BufferedWriter(new FileWriter(toFile));
        String line;
        while ((line = br.readLine()) != null) {
            // 一次读一行,并不能识别换行
            System.out.println(line);
            bw.write(line);
            // 添加该换行时进行换行
            bw.newLine();
            bw.flush();
        }
        br.close();
        bw.close();
    }

    public static void main(String[] args) throws IOException {
        FileCopy.copyFileTwo(new File("src\com\jef\io\blog\file\copy\from.txt"),
                new File("src\com\jef\io\blog\file\copy\to.txt"));
    }

}

转化流:

OutputStreamWriter,InputStreamReader把字节转化成字符。

package com.tfj.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * @author Jacksile
 */
public class IsrAndOsw {

    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("1.txt");
        // 根据文件存储编码设置编码
        InputStreamReader isr = new InputStreamReader(in, "gbk");
        FileOutputStream out = new FileOutputStream("2.txt");
        OutputStreamWriter osw = new OutputStreamWriter(out);

        /*int c;
        while ((c = isr.read()) != -1) {
            System.out.print((char) c);
        }*/

        char[] buffer = new char[8 * 1024];
        int c;
        while ((c = isr.read(buffer, 0, buffer.length)) != -1) {
            String string = new String(buffer, 0, c);
            System.out.print(string);
            // 以字符的形式进行文件的拷贝
            osw.write(buffer, 0, c);
            osw.flush();
        }
        isr.close();
        osw.close();
    }

}

数据流:

DataOutputStream,DataInputStream
因为平时若是我们输出一个8个字节的long类型或4个字节的float类型,那怎么办呢?可以一个字节一个字节输出,也可以把转换成字符串输出,但是这样转换费时间,若是直接输出该多好啊,因此这个数据流就解决了我们输出数据类型的困难。数据流可以直接输出float类型或long类型,提高了数据读写的效率。

打印流:

PrintWriter,PrintStream一般是打印到控制台,可以进行控制打印的地方。

对象流:

ObjectOutputStream,ObjectInputStream把封装的对象直接输出,而不是一个个在转换成字符串再输出。
使用对象流需要实现Serializable接口,否则会报错。而若用transient关键字修饰成员变量,不写入该成员变量,若是引用类型的成员变量为null,值类型的成员变量为0

序列化流:SequenceInputStream。

对象序列化:把对象直接转换成二进制,写入介质中。

原文地址:https://www.cnblogs.com/tufujie/p/5042547.html