Java-Day-24( 文件 + IO流 )

发布时间 2023-05-29 14:37:26作者: 朱呀朱~

Java-Day-24

文件

  • 文件就是保存数据的地方

文件流

  • 文件在程序中是以流的形式来操作的

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

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

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

    • java 程序 ( 内存 ) <—( 输入流 )— ...... ( 如:文件 ( 磁盘 ) )

      java 程序 ( 内存 ) —( 输出流 )—> ...... ( 如:文件 ( 磁盘 ) )

常见的文件操作 ( 方法 )

  • 创建文件对象相关构造器和方法

  • image-20230522234732884

  • image-20230522234848685

创建文件

  • new File ( String pathname ) : 根据路径构建一个 File 对象
public class test1 {
    public static void main(String[] args) {  
        
    //    方式一:new File(String pathname)
    @Test // 注解@Test运行
    public void create01() {
        String filePath = "e:\\new01.txt";
        File file = new File(filePath);
        //        于java来说,仅是在内存里创建了一个对象
        try {
            file.createNewFile();
            //        真正写入硬盘,成为真正的文件
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
      
}
  • new File ( File parent, String child ) :根据父目录文件 + 子路径构建
//    方式二:new File(File parent, String child)
//    e:\\new02.txt
@Test
public void create02() {
    File parentFile = new File("e:\\");
    String fileName = "new02.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)
@Test
public void create03() {
    String parentPath = "e:\\";
    String fileName = "new03.txt";
    File file = new File(parentPath, fileName);
    try {
        file.createNewFile();
        System.out.println("创建成功咧");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

获取文件相关的信息

  • getName、getAbsolutePath、getParent、length ( 大小:字节 )、exists、isFile、isDirectory
@Test
//    获取文件信息
public void info() {
    //        先创建文件的对象
    File file = new File("e:\\new01.txt");
    //        调用相应的方法,得到对应信息
    System.out.println("文件名称 = " + file.getName());
    System.out.println("文件绝对路径 = " + file.getAbsolutePath());
    System.out.println("文件大小(字节) = " + file.length()); // 文件内容:一个汉字三个字节,字母、字符都占一个
    System.out.println("文件是否存在 = " + file.exists()); // true
    System.out.println("是不是一个文件 = " + file.isFile()); // true
    System.out.println("是不是一个目录 = " + file.isDirectory()); // false
}

目录的操作和文件删除

  • mkdir 创建一级目录、mkdirs 创建多级目录、delete 删除空目录或文件

  • 判断 d:\\news01.txt 是否存在,如果存在就删除

@Test
public void m1() {
    String filePath = "e:\\new01.txt";
    File file = new File(filePath);
    if (file.exists()) {
        if (file.delete()) {
            System.out.println(filePath + ":删除成功");
        } else {
            System.out.println(filePath + ":删除失败");
        }
    } else {
        System.out.println("该文件不存在......");
    }
}
  • 判断 E:\\demo02 是否存在 ( E 小写也可 )
@Test
//    在java编程中,目录也被当作文件(特殊的文件)
public void m1() {
    //        String filePath = "e:\\demo02";
    String filePath = "E:\\demo02";
    File file = new File(filePath);
    if (file.exists()) {
        if (file.delete()) {
            System.out.println(filePath + ":删除成功");
        } else {
            System.out.println(filePath + ":删除失败");
        }
    } else {
        System.out.println("该文件不存在......");
    }
}
  • 判断 D:\\demo\\a\\b\\c 目录是否存在,存在就提示,不存在就创建
    • 创建一级目录 file.mkdir()
    • 创建多级目录 file.mkdirs()
@Test
//    在java编程中,目录也被当作文件(特殊的文件)
public void m1() {
    String directoryPath = "E:\\demo\\a\\b\\c";
    //        String directoryPath = "E:\\demo";
    File file = new File(directoryPath);
    if (file.exists()) {
        System.out.println(directoryPath + "目录已经存在");
    } else {
        if (file.mkdirs()) { // 创建多级目录:mkdirs
            //            if (file.mkdir()) { // mkdir是创建一级目录,如:"E:\\demo"
            System.out.println(directoryPath + "该目录创建成功");
        } else {
            System.out.println(directoryPath + "该目录创建失败");
        }
    }
}

Java IO 流的原理

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

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

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

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

  • 输出 output:将程序 ( 内存 ) 数据输出到磁盘、光盘等存储设备 ( 除了一些文件外,也可以是另一个程序、数据库或网络等 ) 中

image-20230528001131597

流的分类

  • 按操作数据单位不同分为:

    • 以字节 ( 一字节等于八位 ) 为单位读取、操作:字节流 ( 8 bit )
      • 二进制文件的话,字节流能保证无损操作
    • 按字符的方式操作:字符流 ( 按字符 )
      • 文本文件的话,字符组织而成,用字符流来比较好
      • 按字符的话,对应字节得根据编码方式来看
      • 操作单位来看,字符流的效率更高 ( 读取单位多些 )
  • 按数据流的流向不同分为:输入流、输出流

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

  • 字节流:字节输入流、字节输出流 ( 字符流同样 )

    • 对应顶级父类分别是:InputStream 和 OutputStream
    ( 抽象基类 ) 字节流 字符流
    输入流 InputStream Reader
    输出流 OutputStream Writer
    • 上面四个类都是抽象类,使用时只能借其实现类
    • Java 的 IO 流共涉及 40 多个类,实际上非常规则,都是从如上四个抽象基类派生的 ( 即为子类 ),可视上述四个为流的顶级基类
    • 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀的
  • 理解:流就相当于快递小哥,不管是用户 ( 程序 ) 发送物品还是接收物品,都要通过快递小哥 ( 流 ) 来作为传输媒介