Java文件与IO流操作原理详细分析

作者:小黎的培培笔录 时间:2023-08-12 13:13:46 

一、文件

1、基本解释

(1)什么是文件?

文件是保存数据的地方,比如大家经常使用的word文档、txt文件、excel文件等都是文件。它还可以保存一张图片,也可以保存视频声音

(2)文件流

Java文件与IO流操作原理详细分析

流 :数据在数据源(文件)和程序(内存)之间经历的路径

输入流 : 数据从数据源(文件)到程序(内存)的路径

输出流 :数据从程序(内存)到数据源(文件)的路径

2、常用的文件操作

(1)相关方法

  • new File(String pathname) --> 根据路径构建一个File对象

  • new File(File parent,String child) --> 根据父目录文件+子路径构建

  • new File(String parent,String child) --> 根据父目录+子路径构建

  • createNewFile 创建新文件

(2)代码示例

(①)方式一:new File(String pathname)

//方式1 new File(String pathname)
   public void create01(){
       //定好路径
       String filePath = "D:\\news1.txt";
       //创建路径对象
       File file = new File(filePath);
       try {
           //根据路径创建文件
           file.createNewFile();
           System.out.println("创建文件成功");
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

(②)方式二:new File(File parent,String child)

//第二种方式 new File(File parent,String child) ,根据父目录文件 + 子路径构建
   //D:\\news2.txt
   public void create02(){
       File parentFile = new File("D:\\");
       String fileName = "news2.txt";
       //创建路径对象
       File file = new File(parentFile, fileName);
       try {
           //根据路径创建文件
           file.createNewFile();
           System.out.println("创建成功~");
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

(③)方式三:new File(String parent,String child)

//第三种方式 new File(String parent,String child) ,根据父目录文件 + 子路径构建
   //D:\\news3.txt
   public void create03(){
       String parentFile = "D:\\";
       String fileName = "news3.txt";
       File file = new File(parentFile, fileName);
       try {

file.createNewFile();
           System.out.println("创建成功~");
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

3、获取文件相关信息

(1)常用方法:代码示例

//获取文件的信息
   public void info(){
       //先创建文件对象
       File file = new File("D:\\news1.txt");
       //调用相应的方法,得到对应的信息
       System.out.println("文件名字=" + file.getName());
       System.out.println("文件绝对路径=" + file.getAbsolutePath());
       System.out.println("文件父级目录=" + file.getParent());
       System.out.println("文件大小(字节)=" + file.length());
       System.out.println("是否有文件存在=" + file.exists());
       System.out.println("是不是有一个文件=" + file.isFile());
       System.out.println("是不是有一个目录=" + file.isDirectory());
   }

4、目录操作和文件删除

  • mkdir 创建一级目录

  • mkdirs 创建多级目录

  • delete 删除空目录或文件

==》代码示例1:

//判断 d:\\news1.txt 是否存在,如果存在就删除
   public void m1(){
       //文件路径
       String filePath = "d:\\news1.txt";
       //创建文件路径对象
       File file = new File(filePath);
       //首先判断文件是否存在
       if (file.exists()){
           //如果存在,就删除
           if (file.delete()){
               System.out.println(filePath + "删除成功");
           }else {
               System.out.println("删除失败");
           }
       }else {
           System.out.println("该文件不存在.....");
       }
   }

==》代码示例2:

//判断 d:\\demo02 是否存在,如果存在就删除
   //这里我们需要体会到,在Java编程中,目录也被当做文件
   public void m2(){
       //文件路径
       String filePath = "d:\\demo02";
       //创建文件路径对象
       File file = new File(filePath);
       //首先判断文件是否存在
       if (file.exists()){
           //如果存在,就删除
           if (file.delete()){
               System.out.println(filePath + "删除成功");
           }else {
               System.out.println("删除失败");
           }
       }else {
           System.out.println("该目录不存在.....");
       }
   }

==》代码示例3:

//判断:D:\\demo\\a\\b\\c 目录是否存在,如果存在就提示已经存在,否则就创建
   public void m3(){
       //文件路径
       String directoryPath = "D:\\demo\\a\\b\\c";
       //路径对象
       File file = new File(directoryPath);
       //首先判断文件是否存在
       if (file.exists()){
           System.out.println(directoryPath + "存在");
       }else {
           if (file.mkdirs()){
               System.out.println(directoryPath + "创建成功");
           }else {
               System.out.println("创建失败");
           }
       }
   }

二、IO流原理及分类

1、IO流原理

  • I / O是 Input / Output的缩写,I/O技术是非常实用的技术,用于处理数据传输,如读文件、写文件,网结通讯等

  • Java程序中,对于数据的输入/输出操作以”流(stream)" 的方式进行。

  • java.io 包下提供了各种“流”的类和接口,用以获取不同种类的数据,并通过方法输入或输出数据

  • 输入(input) : 读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。

  • 输出(output): 将程序(内存)数据输出到磁盘、 光盘等存储设备中

  • 原理示意图:

Java文件与IO流操作原理详细分析

2、流的分类

  • 按操作数据单位不同分为 : 字节流(8 bit)二进制文件,字符流(按字符)文本文件

  • 按数据流的流向不同分为 : 输入流,输出流

  • 按流的角色的不同分为 : 节点流,处理流/包装流

Java文件与IO流操作原理详细分析

注意:

1、Java的I0流共涉及40多个类 实际上非常规则 都是从如上4个抽象基类派生的

2、由这四个类派生出来的子类名称都是以其父类名作为子类名后缀

3、IO流体系图

Java文件与IO流操作原理详细分析

来源:https://blog.csdn.net/yzh2776680982/article/details/125112766

标签:Java,文件,IO流
0
投稿

猜你喜欢

  • springboot项目配置swagger2示例详解

    2021-09-01 04:24:47
  • 使用JAVA实现邮件发送功能的图文教程

    2021-09-04 17:29:36
  • C#使用winform简单导出Excel的方法

    2022-06-11 06:58:49
  • 带你了解mybatis如何实现读写分离

    2023-07-29 14:26:28
  • 基于OpenGL实现多段Bezier曲线拼接

    2022-03-07 03:47:31
  • HTTP中get和post的区别详解

    2023-04-19 11:42:18
  • java和c#使用hessian通信的方法

    2021-12-12 22:03:46
  • Spring Cloud Gateway 拦截响应问题分析(数据截断问题)

    2022-06-20 07:30:27
  • 带你了解Java数据结构和算法之无权无向图

    2023-12-24 10:54:05
  • Windows 10卸载JDK1.8超详细图文教程

    2022-07-07 22:30:16
  • springboot实现maven多模块和打包部署

    2022-01-06 00:41:15
  • 判断图片-判断位图是否是黑白图片的方法

    2023-06-09 17:20:07
  • Spring @Conditional注解原理解析

    2022-10-04 16:09:51
  • 浅谈两个jar包中包含完全相同的包名和类名的加载问题

    2023-04-13 04:47:02
  • 详解Java面向对象编程之多态

    2023-08-28 19:17:06
  • mybatis-plus查询源码详解

    2023-02-02 11:58:02
  • Java数据结构 递归之迷宫回溯案例讲解

    2023-04-01 11:16:38
  • Android应用APP自动更新功能的代码实现

    2022-09-17 09:48:25
  • springboot项目如何设置session的过期时间

    2022-12-31 23:45:27
  • 被kafka-client和springkafka版本坑到自闭及解决

    2023-08-23 15:07:36
  • asp之家 软件编程 m.aspxhome.com