• 《从零开始的Java世界》10File类与IO流


    《从零开始的Java世界》系列主要讲解Javase部分,从最简单的程序设计到面向对象编程,再到异常处理、常用API的使用,最后到注解、反射,涵盖Java基础所需的所有知识点。学习者应该从学会如何使用,到知道其实现原理全方位式地学习,才能为以后框架的学习打下良好的基础。

    目录

    1.File类

    1.1概述

    1.2实例化

    1.2.1构造器

    1.3常用方法

    2.IO流

    2.1概述

    2.2分类

    2.3流的API

    3.文件流(节点流)

    3.1FileReader

    3.2FileWriter

    3.3FileInputStream及FileOutputStream

    4.处理流

    4.1缓冲流

    4.1.1实现步骤

    4.1.2实现代码

    4.2转换流

    4.2.1介绍

    4.2.2实现

    4.3对象流

    4.3.1说明

    4.3.2对象的序列化机制

    4.3.3使用

    序列化过程

    反序列化过程

    ​编辑


    1.File类

    1.1概述

    1.2实例化

    1.2.1构造器

    1.3常用方法

     

    2.IO流

    2.1概述

    2.2分类

    2.3流的API

    3.文件流(节点流)

    3.1FileReader

    读写文本文件用字符流

    1. @Test
    2. public void test(){
    3. FileReader fr = null;
    4. try {
    5. //1.创建File类对象,对应一个文件
    6. File file = new File("hello.txt");
    7. //2.创建输入型的字符流,用于读取数据
    8. fr = new FileReader(file);
    9. //3.读取数据,显示在控制台上
    10. //方式一:
    11. // int data = fr.read();
    12. // while (data != -1){
    13. // System.out.print((char)data);
    14. // data = fr.read();
    15. //方式二:
    16. char buffer[] = new char[5];
    17. int len = fr.read(buffer);
    18. while (len != -1){
    19. for(int i = 0; i < len; i++){
    20. System.out.print(buffer[i]);
    21. }
    22. len = fr.read(buffer);
    23. }
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. } finally {
    27. //4.关闭流资源
    28. try {
    29. fr.close();
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. }

    3.2FileWriter

    1. @Test
    2. public void test2() {
    3. FileWriter fw = null;
    4. try {
    5. //1.创建File对象,指明操作的文件名称
    6. File file = new File("info.txt");
    7. //2.创建输出流
    8. fw = new FileWriter(file);
    9. //3.输出过程
    10. fw.write("ykx666");
    11. } catch (IOException e) {
    12. e.printStackTrace();
    13. }
    14. //4.关闭资源
    15. try {
    16. fw.close();
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }

    3.3FileInputStream及FileOutputStream

    读写非文本文件(如图片、音视频)用字节流

    1. @Test
    2. public void test(){
    3. FileInputStream fis = null;
    4. FileOutputStream fos = null;
    5. try {
    6. //1.创建相关的File类对象
    7. File file1 = new File("sdz.jpg");
    8. File file2 = new File("copy_sdz.jpg");
    9. //2.创建相关字节流
    10. fis = new FileInputStream(file1);
    11. fos = new FileOutputStream(file2);
    12. //3.数据的输入和输出
    13. byte[] buffer = new byte[1024];
    14. int len;
    15. while((len = fis.read(buffer)) != -1)
    16. fos.write(buffer,0,len);
    17. System.out.println("复制成功!");
    18. }catch (IOException e) {
    19. e.printStackTrace();
    20. }
    21. //4.关闭资源
    22. try {
    23. fis.close();
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. try {
    28. fos.close();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }
    32. }

    4.处理流

    4.1缓冲流

    4.1.1实现步骤

    4.1.2实现代码

    1. /*
    2. * 使用BufferedInputStream/BufferedOutputStream复制一张图片
    3. * @author yangkx
    4. * @date 2023-05-22 19:48
    5. */
    6. @Test
    7. public void test(){
    8. FileInputStream fis = null;
    9. FileOutputStream fos = null;
    10. BufferedInputStream bis = null;
    11. BufferedOutputStream bos = null;
    12. try {
    13. //1.创建相关的File类对象
    14. File file1 = new File("sdz.jpg");
    15. File file2 = new File("copy_sdz2.jpg");
    16. //2.创建相关字节流、缓冲流
    17. fis = new FileInputStream(file1);
    18. fos = new FileOutputStream(file2);
    19. bis = new BufferedInputStream(fis);
    20. bos = new BufferedOutputStream(fos);
    21. //3.数据的输入和输出
    22. byte[] buffer = new byte[1024];
    23. int len;
    24. while((len = bis.read(buffer)) != -1)
    25. bos.write(buffer,0,len);
    26. System.out.println("复制成功!");
    27. }catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. //4.关闭资源
    31. //先关外层
    32. try {
    33. bis.close();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. try {
    38. bos.close();
    39. } catch (IOException e) {
    40. e.printStackTrace();
    41. }
    42. try {
    43. fis.close();
    44. } catch (IOException e) {
    45. e.printStackTrace();
    46. }
    47. try {
    48. fos.close();
    49. } catch (IOException e) {
    50. e.printStackTrace();
    51. }
    52. }

    4.2转换流

    4.2.1介绍

    4.2.2实现

    4.3对象流

    4.3.1说明

    4.3.2对象的序列化机制

    4.3.3使用

    序列化过程

    反序列化过程

    内容来源于尚硅谷javase课程的ppt,仅作为学习笔记参考

  • 相关阅读:
    注册成Windows服务
    vue2+axios实现注册页面加载动画消息和配置跨域代理
    安卓界面优化
    设计模式浅析(五) ·单例模式
    新版HI3559AV100开发注意事项
    绘制X-Bar-S和X-Bar-R图,监测过程,计算CPK过程能力指数
    【vulnhub靶场】PRIME:1打靶过程记录
    神经网络中的反向传播:综合指南
    webpack打包js文件
    mysql linux 5.7版本的安装,以及镜像的问题
  • 原文地址:https://blog.csdn.net/weixin_44572229/article/details/138122804