Java流

2020-04-23  本文已影响0人  抬头挺胸才算活着

参考资料:
[1]. 传智黑马IDEA版本2019Java教学视频---多线程与IO流

字节流与字符流

字节不涉及编码,只是一堆二进制;
字符需要在字节的基础上用某种编码规范,将字节(人类看不懂)解码为字符(人类看得懂)的。


字节流FileOutputStream创建文件

字节流子类
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Stream {
    public static void main(String[] args) throws IOException {
        // FileOutputStream的构造器可以输入文件名字或者输入文件路径
//        FileOutputStream fos = new FileOutputStream(".\\a.txt");
        FileOutputStream fos = new FileOutputStream(new File(".\\a.txt"));

        fos.write(97);
        fos.write('b');
        fos.write("\r\n".getBytes());
        fos.write("你好".getBytes());
        fos.close();
    }
}

FileInputStream读取文件

注意,为了正确地显示,需要将读取出来的字节转化为字符串或者字符。

    private static String readFile(String filename) throws IOException {
        FileInputStream fis = new FileInputStream(filename);
        byte[] buff = new byte[1024];
        int len = fis.read(buff);
        fis.close();
        return new String(buff, 0, len);
    }

文件复制

    private static void copyFile(String from, String to) throws IOException {
        FileInputStream fis = new FileInputStream(from);
        FileOutputStream fos = new FileOutputStream(to);

        byte[] buff = new byte[1024];
        while(true){
            int len = fis.read(buff);
            if(len==-1){
                break;
            }else{
                fos.write(buff, 0, len);
            }
        }

        fis.close();
        fos.close();
    }

FileReader

继承:FileReader->InputStreamReader(这个涉及编码,后面讲)->Reader。
FileReader对比InputStreamReader的区别是:前者读取的是一个个的字符,后者是一个个的字节。
FileReader底层使用FileInputStream来读取字节流!!

    public FileReader(String fileName) throws FileNotFoundException {
        super(new FileInputStream(fileName));
    }
    private static String readFile2(String filename) throws IOException {
        FileReader fr = new FileReader(filename);
        char[] buff = new char[1024];
        int len = fr.read(buff);
        fr.close();
        return new String(buff, 0, len);
    }

FileWriter

继承:FileWriter->OutputStreamWriter(这个涉及编码,后面讲)->Writer
FileWriter对比FileOutputStream除了字符和字节的区别区别外,FileWriter在写入的时候先把字符转化为字节写入到内存缓存区中,然后再flush到硬盘,如果不调用flush或者close的话,不会写入到硬盘中去。

缓存流(buffered)

对上面四种基本的流进行一种增强,会创建一个内置的默认大小的缓存区数组,通过缓存区读写,减少系统IO次数。
命名规律:在增强的流类型的基础上加上Buffered
字节缓存流:BufferedInputStream,BufferedOutputStream
字符缓存流:BufferedReader,BufferedWriter

BufferedOutputStream

继承:BufferedOutputStream->OutputStream
创建的时候需要指定输出流,写入之后,需要调用flush或者close刷新到磁盘。

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("1.txt"));
bos.write("你好".getBytes());
bos.flush();
bos.close();

编码流

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("1.txt"), "utf-8");
osw.write("你好");
osw.flush();
osw.close();

两种编码的转化

    void GBK2UTF8(String from, String to) throws IOException{
        InputStreamReader isr = new InputStreamReader(new FileInputStream(from), "GBK");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(to), "UTF-8");
        int ch;
        while((ch = isr.read())!=-1){
            osw.write(ch);
        }
        isr.close();
        osw.close();
    }
上一篇下一篇

猜你喜欢

热点阅读