Java IO流

个人笔记 

一、什么是IO流?

也就是In和Out输入和输出,也就是程序和外部设备之间的数据传递(文件、管道、网络连接)

流(Stream)是指一连串的数据(字节或者字符)通过先进先出的方式发送信息的通道(而数据又可以是文件、内存、网络连接);

二、IO流的分类

  • 按数据流的方向:输入流、输出流

  • 按处理数据单位:字节流、字符流

  • 按功能:节点流、处理流

三、字节流

public class 字节复制 {
    public static void main(String[] args) throws IOException {
        //选择要从哪个文件输入;
        File infile = new File("C:\\Users\\1\\Desktop\\三笠.jpg");
        //选择要输出到那个文件
        File outFile = new File("C:\\Users\\1\\Desktop\\三笠2.jpg");
        //字节输入流
        FileInputStream fis = new FileInputStream(infile);
        //字节输出流
        FileOutputStream fos = new FileOutputStream(outFile);
        //字节输入缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
        //字节输出缓冲流
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //一边输入一个输出
        //自定义缓存区
        byte[] bytes = new byte[2];
        int data = 0;
        while ((data=bis.read(bytes))!=-1){
            bos.write(bytes,0,data);
        }
        //关闭流
        bis.close();
        bos.close();
        System.out.println("复制成功");
    }
}

四、字符流

 public static void main(String[] args) throws IOException {

        //读取的文件
        File fie = new File("C:\\Users\\1\\Desktop\\CmdInfo.txt");
        //写入到哪个文件
        File foe = new File("C:\\Users\\1\\Desktop\\CmdInfo2.txt");
        
        //读取流
        FileReader fr = new FileReader(fie);
        //字符读取缓冲
        BufferedReader br = new BufferedReader(fr);
        
        //写入流
        FileWriter fw = new FileWriter(foe);
        //字符写入缓冲
        BufferedWriter bw = new BufferedWriter(fw);
        
        //操作
        int data = 0;
        char[] chars = new char[64];
        while ((data= br.read(chars))!=-1){
            bw.write((new String(chars,0,data)));
        }
        //关
        br.close();
        bw.close();
        System.out.println("写入成功");
    }

五、字节流与字符流的转换

public static void main(String[] args) throws IOException {
        //选择需要读取的文件
        File file = new File("C:\\Users\\1\\Desktop\\CmdInfo.txt");
        //字节读取
        FileInputStream fis = new FileInputStream(file);

        //字节转化字符类型
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");

        BufferedReader br = new BufferedReader(isr);

        int data=0;
        StringBuilder sb = new StringBuilder();
        while ((data=br.read())!=-1){
            sb.append((char) data);
        }
        System.out.println(sb.toString());
        br.close();
    }

六、打印流

    public static void main(String[] args) throws FileNotFoundException {
        //选择需要写入到哪个文件
        File file = new File("C:\\Users\\1\\Desktop\\CmdInfo2.txt");
        //打印流
        PrintWriter pw = new PrintWriter(file);
        pw.println(7144);
        pw.println(true);
        pw.println('x');
        //关闭打印流
        pw.close();
        System.out.println("OK");
    }

七、序列化与反序列化

public class 序列化与反序列化 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        SerFile();
        Thread.sleep(1000);
        FSerFile();
    }

    public static void SerFile() throws IOException {
        //选择需要写入到哪个文件
        File file = new File("C:\\Users\\1\\Desktop\\peo.bin");
        //字节写入流
        FileOutputStream fos = new FileOutputStream(file);
        //对象流
        ObjectOutputStream ojs = new ObjectOutputStream(fos);
        //序列化
        Person ps = new Person("罗罗诺亚",20);
        ojs.writeObject(ps);
        //关闭
        ojs.close();
        System.out.println("成功");
    }

    public static void FSerFile() throws IOException, ClassNotFoundException {
        //选择需要读取到哪个文件
        File file = new File("C:\\Users\\1\\Desktop\\peo.bin");
        //输入流
        FileInputStream fis = new FileInputStream(file);
        //对象流
        ObjectInputStream ois = new ObjectInputStream(fis);
        //反序列化
        Person ps = (Person) ois.readObject();
        //关闭流
        ois.close();
        System.out.println(ps);
        System.out.println("OK");
    }

}
class Person implements Serializable {
    private String name;
    private int age;

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

    public Person() {
    }

    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 "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 注意:在序列化一次后,该序列化对象会有一个唯一的ID;所以反序列化的时候ID也必须是同样的才能序列化成功;且在序列化的类要实现一个标志接口;

八、File类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值