7、IO流


Java零基础极速入门-讲师:海波

失败,是正因你在距成功一步之遥的时候停住了脚步。


1、数据流处理

在这里插入图片描述

public class Demo {
    public static void main(String[] args) throws Exception {
        // 数据 + 流(转)操作
        // 数据从哪里来,到哪里去
        // I:Input,输入(In)
        // O:Output,输出(Out)
        // Stream:流转
    }
}

2、文件流

public class Demo {
    public static void main(String[] args) throws Exception {
        // 文件流
        // File:文件类型,属于java.io
        // 创建对象:使用文件路径关联系统文件
        String filePath = "D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt";
        File file = new File(filePath);
        // 文件对象的操作
        // 判断当前文件是否为文件
        //file.isFile();
        // 判断当前文件是否为文件夹
        //file.isDirectory();
        // 判断当前文件是否在关联(能否找到当前文件)
        //file.exists();

        if (file.exists()) {
            // 文件存在
            System.out.println("文件存在");
            if(file.isFile()){
                System.out.println("文件对象关联的是一个文件");
                file.getName();
                file.length();
                file.lastModified();
                file.getAbsoluteFile();
            }else if(file.isDirectory()){
                System.out.println("文件对象关联的是一个文件夹");
                file.getName();
                file.lastModified();
                file.getAbsoluteFile();
                // 查看文件夹下文件
                String[] list = file.list();
                System.out.println("文件夹中文件");
                for (String s : list) {
                    System.out.println(s);
                }
                // 查看文件夹下文件对象
                File[] files = file.listFiles();
                System.out.println("文件夹中文件对象");
                for (File file1 : files) {
                    System.out.println(file1);
                }

            }
        } else {
            // 文件不存在
            System.out.println("文件不存在");
            // 创建多级文件目录
            //file.mkdirs();
            // 创建文件
            file.createNewFile();
        }
    }
}

3、文件复制

public class Demo {
    public static void main(String[] args) {
        // 文件复制

        // 数据源文件对象
        File srcFile = new File("D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt");
        // 数据目的地文件对象(没有创建文件,会自动创建)
        File destFile = new File("D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt.copy");
        // 文件输入流(管道对象)
        FileInputStream is = null;
        // 文件输出流(管道对象)
        FileOutputStream os = null;
        int num = 0;
        try {
            //  文件输入流创建,放入要操作文件对象(管道对象)
            is = new FileInputStream(srcFile);
            // 文件输出流创建,放入要操作文件对象(管道对象)
            os = new FileOutputStream(destFile);
            // 打开输入阀门,流转数据(输入)
            while (((num = is.read()) != -1)) {
                // 打开输出阀门,流转数据(输出)
                os.write(num);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

4、缓冲流

public class Demo {
    public static void main(String[] args) {
        // 文件复制

        // 数据源文件对象
        File srcFile = new File("D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt");
        // 数据目的地文件对象(没有创建文件,会自动创建)
        File destFile = new File("D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt.copy");
        // 文件输入流(管道对象)
        FileInputStream is = null;
        // 文件输出流(管道对象)
        FileOutputStream os = null;

        // 输入缓冲区
        BufferedInputStream bis = null;
        // 输出缓冲区
        BufferedOutputStream bos = null;

        int num = -1;

        // 设置缓冲区大小
        byte[] bytes = new byte[1024];

        try {
            //  文件输入流创建,放入要操作文件对象(管道对象)
            is = new FileInputStream(srcFile);
            // 文件输出流创建,放入要操作文件对象(管道对象)
            os = new FileOutputStream(destFile);
            // 传入缓冲区
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(os);

            // 打开输入阀门,流转数据(输入)
            while (((num = bis.read(bytes)) != -1)) {
                // 打开输出阀门,流转数据(输出)
                bos.write(bytes, 0, num);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

5、字符流

public class Demo {
    public static void main(String[] args) {
        // 文件复制

        // 数据源文件对象
        File srcFile = new File("D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt");
        // 数据目的地文件对象(没有创建文件,会自动创建)
        File destFile = new File("D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt.copy");
        // 通过字符的方式读取文件
        FileReader fr = null;
        // 字符输入流(管道对象)
        BufferedReader is = null;
        // 字符输出流(管道对象)
        PrintWriter os = null;

        try {
            fr = new FileReader(srcFile);
            //  文件输入流创建,放入要操作文件对象(管道对象)
            is = new BufferedReader(fr);
            // 文件输出流创建,放入要操作文件对象(管道对象)
            os = new PrintWriter(destFile);

            // 读取文件中一行数据(字符串)
            String data = null;
            // 打开输入阀门,流转字符(输入)
            while (((data = is.readLine()) != null)) {
                // 打开输出阀门,流转数据(输出)
                os.println(data);
            }
            // 刷写数据
            os.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (os != null) {
                os.close();
            }
        }

    }
}

6、序列化

在这里插入图片描述

在这里插入图片描述

6.1、序列化代码

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

        // 数据源文件对象
        File dataFile = new File("D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt");

        FileOutputStream os = null;
        ObjectOutputStream oos = null;

        try {
            os = new FileOutputStream(dataFile);
            oos = new ObjectOutputStream(os);
            User user = new User();
            oos.writeObject(user);
            // 刷写数据
            os.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

class User implements Serializable {

}

6.2、反序列化代码

public class Demo {
    public static void main(String[] args) {
        // 文件复制

        // 数据源文件对象
        File dataFile = new File("D:\\ProgramData\\IdeaWorkspace\\java-top-speed\\src\\main\\resources\\word.txt");

        FileInputStream is = null;
        ObjectInputStream ois = null;

        try {
            is = new FileInputStream(dataFile);
            ois = new ObjectInputStream(is);
            Object o = ois.readObject();
            System.out.println(o);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

class User implements Serializable {

}

7、常见异常

public class Demo {
    public static void main(String[] args) {
        FileInputStream is = null;
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {
            is = new FileInputStream("xxx"); //FileNotFoundException
            is.read(); //IOException
            oos.writeObject(); //NotSerializableException
            ois.readObject(); //ClassNotFoundException
        } catch (Exception e) {
        } finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值