IO流的概述

本篇文章记录下所学的IO流

File对象

基本方法:createNewFile(创建文件),mkdir(创建目录),mkdirs(如父目录不存在,则全部创建,创建多级目录)
delete(删除目录或文件),删除目录时,如果目录中有文件就不能直接删除,需要先删除内容。

进阶方法:isDirectory,isFile,exists,getAbsolutePath,getPath(将抽象路径名转化为路径名字字符串)
getName,list(返回抽象路径名表示的目录下的文件和目录字符串数组),
listFiles(返回抽象路径名表示的目录下的文件和目录File对象数组)。

编码解码问题

​ 一个汉字存储,如果是GBK编码,占用2个字节,如果是UTF-8编码,占用3个字节
按照某种规则,将字符存储到计算机中,称为编码,反之,将存储在计算机中的二进制数按某种规则解析显示出来,称为解码按照A编码存储,必须按照A编码解析,这样才能显示正确的文本,否则会导致乱码
​ 字符编码:就是一套自然语言的字符与二进制数之间的对应规则(A 65)字符集,是一个系统支持的所有字符的集合,计算机要准确的存储和识别各种字符集符号,就需要进行字符编码
常见的字符集:
Ascll(基于拉丁字母的一套电脑编码系统,7位表示一个字符,共128个字符),
​ Ascll的扩展字符集使用8位表示一个字符,共256字符
GBXXX字符集,简体中文码表,大于127的字符就是全角字符,小于等于127就是半角字符
​ GBK,最常用的中文码表
Unicode字符集,也叫统一码,万国码,有三种编码方案,UTF-8,UTF-16,UTF-32
​ UTF-8最常用,使用1-4字节为每个字符编码
以上就是全部描述,我的理解是:
​ 计算机上的数据文件都是以二进制存放的,但二进制数据却是根据不同的字符编码排列的,同一个字符串,我们使用GBK的方式编码得到二进制文件和使用UTF-8的方式编码得到的二进制文件是完全不同的,如果我们编码使用GBK的方式,解码使用UTF-8的方式,就会导致乱码,因为两者编码解码规则不同

IO流结构

字节流基类:InputStream,OutputStream
它的常用子类有,FileInputStream,FileOutputStream(追加,在FileOutputStream的构造方法中使用true)
BufferedInputStream,BufferedOutputStream

字符流基类:Reader,Writer
它的常用子类有, BufferedReader,BufferedWriter(缓冲流都需要flush)
InputStreamReader,OutputStreamWriter(转换流)
转换流的子类:FileReader,FileWriter(继承转换流,除不能设置编码,其他一样)

除了以上各种流,还有以下几种:
打印流
字节打印流PrintStream,字符打印流PrintWriter

对象序列化流
序列化流ObjectOutputStream,构造方法要字节流,给文件输入输出流即可
序列化方法:writeObject(Object obj)
反序列化流ObjectInputStream
反序列化方法,readObject(),返回自定义类对象
注:1)对于要被序列化的自定义类,需要自定义类实现Serializable接口,并设置SerialVersionID字段
以保证在反序列化时不会出现版本不一致。
2)对于自定义类中不想被序列化的属性,可以加transient关键字

Properties类
底层是一个HashTable集合
特殊方法,setProperty,getProperty,stringPropertyNames(返回键的集合,其中键和对应的值都是字符串)
与IO相结合的方法,load(把文件中的数据加载到集合中),store(把集合中的数据保存到文件)

开发中常用的输出输出方法

这里我们分两类讨论,字节流的输入输出,字符流的输入输出

字节流的输入输出

BufferedInputStream bufferedInput=null;
     BufferedOutputStream bufferedOutput=null;
     try {
             bufferedInput=new BufferedInputStream(new 
                     FileInputStream("C:\\Users\\DELL\\Desktop\\wky.txt"));
             bufferedOutput=new BufferedOutputStream(new 
                     FileOutputStream("C:\\Users\\DELL\\Desktop\\wky2.txt"));
             byte[] bytes = new byte[1024];
             int len=0;
             while ((len=bufferedInput.read(bytes))!=-1){
                 bufferedOutput.write(bytes);
                 bufferedOutput.flush();
             }
         }catch (IOException e) {
             e.printStackTrace();
         }finally {
             if (bufferedOutput!=null){
                 try {
                     bufferedOutput.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
             if (bufferedInput!=null){
                 try {
                     bufferedInput.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
     	}

字符流的输入输出

BufferedReader bufferedReader=null;
        BufferedWriter bufferedWriter=null;
        try {
            /*
            	不需要编码转换就给字符缓冲流FileReader,FileWriter
                bufferedReader = new BufferedReader(new 
                	FileReader("C:\\Users\\DELL\\Desktop\\wky.txt"));    
                bufferedWriter = new BufferedWriter(new 
                    FileWriter("C:\\Users\\DELL\\Desktop\\wky3.txt"));
            */
            bufferedReader = new BufferedReader(new InputStreamReader(new 
            	FileInputStream("C:\\Users\\DELL\\Desktop\\wky.txt"),"GBK"));
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new 
            	FileOutputStream("C:\\Users\\DELL\\Desktop\\wky3.txt"),"GBK"));
            String line;
            while ((line=bufferedReader.readLine())!=null){   //读到文件末尾返回null
                bufferedWriter.write(line);
                bufferedWriter.newLine();   //字符缓冲输入流不读取换行符,需要手动写入换行符
                bufferedWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedWriter!=null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }