Java学习路程之转换流、高效流和Properties集合、序列化流

一.转换流
1.分类:
OutputStreamWriter(字符流转向字节流的桥梁)
InputStreamReader(字节流转向字符流的桥梁)
2.转换流程
1).将程序中的字符,按照创建转换流时给出的编码格式,去查对应的码表
2).将查到的字节交给创建转换流时传入的字节流
3)最终使用字节流将文件写入

public class Demo01 {
    public static void main(String[] args) throws IOException{
        //创建文件字节流
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test/biu.txt");
        //创建转换流
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        osw.write("滚滚长江东逝水");
        //关闭资源,有多个流嵌套时,只需关闭最外面的流即可,自己创建的流要自己关闭,从系统中获取的,不用自己关闭
        osw.close();

    }
    //转换流写GBK格式文件
    public static void writerGBKFile() throws IOException {
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test/biu.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        osw.write("飞流直下三千尺,疑是银河落九天");
        osw.close();
    }
    //读取UTF8格式文件
    public static void readerUTF8() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test/bu.txt");
        InputStreamReader isr = new InputStreamReader(fis);
        char[] c = new char[1024];
        int len = 0;
        while ((len = isr.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }
        isr.close();
    }
    //读取GDK编码格式
    public static void readerGBKFile() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test/biu.txt");
        InputStreamReader isr = new InputStreamReader(fis);
        char[] c = new char[1024];
        int len = 0;
        while ((len = isr.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }
        isr.close();
    }
}

二.高效流
1.
高效字节流:
BufferedOutputStream 写入
BufferedInputStream 读取
高效流自带缓冲数组,缓冲区默认大小为8k,可以提高读取效率

public static void main(String[] args) throws IOException {
        //创建缓冲流写入
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test/xiao.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write("知之为知之,不知为不知,是知也".getBytes());
        bos.close();
        //缓冲流读取
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test/xiao.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = bis.read(b)) != -1) {
            bis.read(b, 0, len);
        }
        bis.close();
    }
public class Day20 {
    public static void main(String[] args) throws IOException {
        //用模板模式比较字节输出流复制文件和高效流复制文件
        //new CopyFile().getTime();
        new CopyFi().getTime();
    }
}
abstract class Copy{
    //源文件路径
    public File src = new File("/Users/lanou/Desktop/test/java入门.pptx"); 
    //目标文件路径
    public File desc = new File("/Users/lanou/Desktop/heihei/java入门.pptx");
    //计算时间
    public void getTime() throws IOException {
        long start = System.currentTimeMillis();
        copyDir();
        long end = System.currentTimeMillis();
        //打印程序运行时间
        System.out.println(end - start);
    }
    //抽象方法
    public abstract void copyDir() throws IOException;
}
//字节输出流复制文件
class CopyFile extends Copy{
    @Override
    public void copyDir() throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = fis.read(b)) != -1) {
            fos.write(b, 0, len);
        }
        fis.close();
        fos.close();
    }
}
//高效流复制文件
class CopyFi extends Copy{
    @Override
    public void copyDir() throws IOException {
        FileInputStream fis = new FileInputStream(src);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(desc);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = bis.read(b)) != -1) {
            bos.write(b, 0, len);
        }
        bis.close();
        bos.close();
    }
}

2.高效字符流
BufferedWriter
BufferedReader

public class Demo02 {
    public static void main(String[] args) throws IOException {
        //高效字符流写入
        FileWriter fw = new FileWriter("/Users/lanou/Desktop/test/xi.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("举头望明月");
        //换行
        bw.newLine();
        bw.write("低头思故乡");
        bw.flush();
        bw.close();

        //高效字符流读取
        FileReader fr = new FileReader("/Users/lanou/Desktop/test/xi.txt");
        BufferedReader br = new BufferedReader(fr);
        String str = "";//接收读出的数据
        //读取一行字符串方法,读到末尾是null
        while ((str = br.readLine()) != null) {
            //该方法不能读取到换行符
            System.out.println(str);
        }
        br.close();
    }
}

3.AutoCloseable 接口

public class Day20 {
    public static void main(String[] args) {

        try (
                //流都实现了这个接口当程序结束时该流对象也会调用自己本类中重写close()方法
                //注意不实现AutoCloseable这个接口,不能在括号里使用
                FileReader fReader = new FileReader("");
                MyCloseable myCloseable = new MyCloseable();
                ){

        } catch (Exception e) {
        }
    }
}
class MyCloseable implements AutoCloseable{
    public void close() throws Exception {
        System.out.println("哈哈哈");
    }
}

三.Properties 集合
1.Properties是双列集合是Hashtable的子类.该集合是唯一与IO流有关系的集合
注意:一般该集合只存储字符串类型的键值对
2.方法
load()将文件直接读入到集合中
store()将集合中的键值对直接写入到文件中
3.读取文件的格式
1).一个key对应一个value并且等号前后不加空格
2).一般该文件使用properties结尾为后缀,起标识作用
3).#为注释

//将文件直接读入集合中
public static void main(String[] args) throws IOException{
        //将文件直接读入集合中
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test/xi.properties");
        //读取文件
        properties.load(fis);
        Set<Object> keySet = properties.keySet();
        //迭代器遍历
        Enumeration<?> enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            //取出所有key
            String key = (String)enumeration.nextElement();
            System.out.println(key + "=" + properties.getProperty(key));

        }
        fis.close();
        //将集合中的元素写入文件
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test/yu.properties");
        Properties properties = new Properties();
        //保存键值对
        properties.setProperty("name", "wangwu");
        properties.setProperty("gender", "man");
        //写入到文件中
        //参数2  是传入的注释
        properties.store(fos, "ha");
        fos.close();
    }

四.序列化流
ObjectOutputStream 写入
ObjectInputStream 读取

public class Day20 {
    public static void main(String[] args) {
        //将对象写入文件
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test/obj.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        //写入
        //NotSerializableException要保存的对象没有实现序列化接口
        oos.writeObject(new Person("lisi", 18));
        oos.close();

        //读取文件
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test/obj.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        //将文件中的对象进行反序列化需要依赖存入对象的.class文件
        Object object = ois.readObject();
        ois.close();
        System.out.println(object);
    }
}

猜你喜欢

转载自blog.csdn.net/l710820742/article/details/82709067
今日推荐