Java---------------------Io流

目录

一,Filen类操作文件或目录

二,流的分类 

 三,流的操作

 四,序列化反序列化


一,Filen类操作文件或目录

 常用方法

二,流的分类 

 按操作数据单位分:字节流(图片,视频之类的非文本文件) , 字符流

按数据的流向分:输入流 , 输出流

按流的角色分:节点流 , 处理流

 

 三,流的操作

 1)造流,造文件

2)读写文件

3)关闭流

案例一 

package com.bdqn.lianxi.ioliu;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class LianXi {
    /**
     * 文件读入read()读入方法
     * @param args
     */

  /*  public static void main(String[] args) {
        FileReader fr = null;
        try {
            //1.实例化File类对象,指明要操作的文件
            File file = new File("day01\\hall.txt");

            //2.提供具体的流
            fr = new FileReader(file);

            //3.数据读入
            //可以造一个数组容器效率高点
            char[]cbuf = new char[5];
            int len;
            while ((len = fr.read(cbuf)) != -1){
                //方法一
                for (int i = 0; i < len;i++){
                    System.out.print(cbuf[i]);
                }
                //方法二
            String str = new String(cbuf,0,len);
                System.out.print(str);
                //错误
//                String str1 = new String(cbuf);
//                System.out.print(str1);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.关闭流操作
                if (fr != null)
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }*/

    /**
     * 文件读出的方法write()方法
     */

    public static void main(String[] args) {
        FileWriter fw = null;//如果不加true是对原有文件覆盖,加true不会覆盖而是在原有文件的基础上追加
        try {
            //1,实例化File类对象,指明要操作的文件
            File file = new File("hell.txt");

            //2,提供FileWrite的对象,用于数据写出
            fw = new FileWriter(file);

            //3,写出文件操作
            fw.write("I have a dream\n");
            fw.write("you need have to a dream");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4,流资源的关闭
                if (fw != null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}

 案例二

package com.bdqn.lianxi.ioliu;

import java.io.*;

public class LianXi1 {
    /**
     * filereadew() 和 filewriter()方法实现文本的复制
     * 非文本的要用FileInputStream()  ,  FileOutputStream()
     * 这四个方法是四个节点流
     */

   /* public static void main(String[] args) {
        FileReader fr = null;
        FileWriter fw = null;

        try {
            //1,创建File类的对象,指明读入和写入的文件
            File re = new File("hell.txt");
            File wi = new File("heel1.txt");

            //2,创建输入流和输出流的对象
            fr = new FileReader(re);
            fw = new FileWriter(wi);

            //3,数据的读入和写出
            char [] arr = new char[5];
            int len;//记录每次读到arr数组中的字符个数
            while ((len = fr.read(arr)) !=-1){
                fw.write(arr,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4,关闭流资源
            try {
                if (fw != null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fr != null)
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/
    public static void main(String[] args) {
       /* File file = new File("day01\\hall.txt");
        File file1 = new File("hall9");

        FileReader fil = new FileReader(file);
        FileWriter fwi = new FileWriter(file1);

        BufferedReader buf = new BufferedReader(fil);
        BufferedWriter buf1 = new BufferedWriter(fwi);*/


        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(new File("day01\\hall.txt")));

            bw = new BufferedWriter(new FileWriter(new File("hall9.txt")));

            char[]chars = new char[5];
            int len;
            while ((len = br.read(chars)) != -1){
                bw.write(chars,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                  br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bw != null)
                  bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

 封装复制方法

package com.bdqn.lianxi.ioliu;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FangFa {
    /**
     * @description:读写方法,可以直接调用
     * @author: liuhang
     * @date: 2022/8/12 14:56
     * @param: in
    out
     **/

    public static void  copy(String in,String out){

        FileInputStream buf = null;
        FileOutputStream bufout = null;
        try {

            File file = new File(in);
            File file1 = new File(out);

            buf = new FileInputStream(file);
            bufout = new FileOutputStream(file1);

            byte[]bytes = new byte[5];
            int end;
            while ((end = buf.read(bytes)) != -1){
                bufout.write(bytes,0,end);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (buf != null)
                     buf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bufout != null)
                      bufout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    public static void main(String[] args) {
        /**
         * @description:调用展示并记录所用时间
         * @author: liuhang
         * @date: 2022/8/12 14:57
         * @param: args
         **/

        long start = System.currentTimeMillis();
        String srcPate = "day01\\hall.txt";
        String despate = "hall.txt7";
        copy(srcPate,despate);
        long end = System.currentTimeMillis();
        System.out.println("花费的时间是:" + (end - start));

    }
}

 转换流与标准输入输出流

package com.bdqn.lianxi.ioliu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BiaoZunDuRuLiu {

    /**
     * 两个转换流
     */

   /* public static void main(String[] args) {
        InputStreamReader re = null;
        OutputStreamWriter wi = null;
        try {
            re = new InputStreamReader(new FileInputStream(new File("day01\\hall.txt")),"utf-8");
            wi = new OutputStreamWriter(new FileOutputStream(new File("hall12.txt")),"gbk");

            char[]chars = new char[20];
            int len;
            while ((len = re.read(chars)) != -1){
                wi.write(chars,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (re != null)
                re.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (wi != null)
                wi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/
    public static void main(String[] args) {

        /**
         * @description:标准输入输出流从控制台输入字符串转换为大写
         * @author: liuhang
         * @date: 2022/8/10 20:14
         * @param: args
         **/

        BufferedReader baf = null;
        try {
            InputStreamReader ir = new InputStreamReader(System.in);
            baf = new BufferedReader(ir);

            while (true){
                System.out.println("请输入字符串");
               String date = baf.readLine();
               if ("e".equals(date) || "exit".equalsIgnoreCase(date)){
                   System.out.println("程序结束");
                   break;
               }else {
                   String upp = date.toUpperCase();
                   System.out.println(upp);
               }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 四,序列化反序列化

 序列化是将对象的状态写入到特定的流中的过程

反序列化则是从特定的流中获取数据重新构建对象的过程

 例:序列化Student类

 Student类需要满足如下条件,方可序列化

1,需要实现Serializable接口

 2,当前类提供一个全局变量:serialVersionUID

 public static final long serialVersionUID = 4567876L;

 3,除当前类需要实现Serializable接口之外,还必须保证其内部所有属性也必须是可序列化的(默认情况下,基本数据类型可序列化)

 Student类:

package com.bdqn.lianxi.ioliu.xuleihuafanxuleihua;

import java.io.Serializable;

public class Student implements Serializable {
    public static final long serialVersionUID = 4567876L;

    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
序列化:
package com.bdqn.lianxi.ioliu.xuleihuafanxuleihua;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @description:序列化
 * @author: liuhang
 * @date: 2022/8/15 16:37
 * @param: null
 **/
public class Serialize {

    public static void main(String[] args) {
        ObjectOutputStream outpu = null;
        try {
            outpu = new ObjectOutputStream(new FileOutputStream("day01//name7"));

            outpu.writeObject(new Student("张三",23));

            outpu.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (outpu != null)
                outpu.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

 反序列化:

package com.bdqn.lianxi.ioliu.xuleihuafanxuleihua;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @description:反序列化
 * @author: liuhang
 * @date: 2022/8/15 16:38
 * @param: null
 **/
public class Deserialization {
    public static void main(String[] args) {

        ObjectInputStream inpu = null;
        Student st = null;
        try {
            inpu = new ObjectInputStream(new FileInputStream("day01//name7"));

            st = (Student) inpu.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {

            try {
                if (inpu != null)
                inpu.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(st);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值