javaIO流---看看这些关于文件的操作你都懂了么

1年前 (2023) 程序员胖胖胖虎阿
149 0 0

javaIO流---看看这些关于文件的操作你都懂了么个人介绍


大家好我是:一颗松
认真分享技术,记录学习点滴
如果分享对你有用请支持我哦🍺

点赞:👍 留言:✍收藏:⭐️

个人格言: 想法落实的最佳时机就是现在!🏄


文章目录

    • IO流思维导图
      • 🗂️1 文件
      • 📝2 文件操作
      • 🌰案例1---用代码创建文件
      • 🌰案例2--- 获取当前文件对象的文件名
      • 🌰案例3--- 判断E盘aba目录下是否存在testaaa.txt,如果存在删掉
      • 🌰案例4---判断e盘下atest目录是否存在,如果存在删除
      • 📊3 javaIO流原理及分类
      • 🌰案例5---FileInputStream读取文件数据
      • 🌰案例6--- 写入文件案例
    • 结语

IO流思维导图

IO知识点多且复杂,对于后期项目优化有重要作用,弄懂IO流是十分必要的

javaIO流---看看这些关于文件的操作你都懂了么

🗂️1 文件

1.1 什么是文件❓

生 活 中:通常指纸质材料,或者官方通知
计算机中: 文件指一个保存数据的空间;比如音频文件、图片文件、视频文件、文本文件这些统称文件。

1.2 什么是文件流❓如何理解流的概念❓

💡答: 在程序中文件是以流的形式来操作的;
流:指文件与程序(内存)之间的经历路径
输入流:数据从外部存储设备到程序(内存)的路径(只能读)
输出流:数据从程序(内存)到外部存储设备的路径(只能写)
输入输出是针对程序本身而言的,指向程序则为输入流,指向文件则为输出流

javaIO流---看看这些关于文件的操作你都懂了么
1.3 流的作用💡

作用1:程序读取文件中的数据
作用2:程序把数据写到文件中

📝2 文件操作

👀2.1 快速了解java File类

在java中提供了文件操作API,我们可以通File类创建对象进而操作文件

  • 拿到File类我们发现它在 java.io包下,不在java.lang包下使用时需要导包
  • File常用的构造方法如下表:
构造器 说明
File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例。
File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例。
  • 看成员方法;
方法名 说明
String getName() 返回此文件或目录的名称。

注意:【如果没有指定路径,只指定文名的情况下,会在当前项目目录下操作】

🆕2.2 创建文件

🌰案例1—用代码创建文件


public static void main(String[] args) throws IOException {
        System.out.println("-------------方式1-------------------");
//        String filePath = "E:\\aba\\test01.txt";
//        creat02(filePath);
        System.out.println("---------------方式2-----------------");
//        String fileName = "test02.txt";
//        creat01(fileName);
        System.out.println("----------------方式3----------------");
//        String parentName = "E:\\aba\\";
//        String fileNaem = "test03.txt";
//        creat03(parentName,fileNaem);
}
//具体方法实现        
 //第一种方式 File(File parent, String child) 创建文件对象
    public static void creat01(String fileName){
        File parentFile = new File("E:\\aba\\");
        File file = new File(parentFile,fileName);
        try {
            file.createNewFile();
            System.out.println("方式1创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //第二种方式 File(String pathname) 创建文件对象
    private static void creat02(String filePath) {
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("方式2创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //第三种方式 File(String parent, String child) 创建文件对象
    private static void creat03(String parentName, String fileNaem) {
        File file = new File(parentName,fileNaem);
        try {
            file.createNewFile();
            System.out.println("第3种方式创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

🔍2.3 获取文件相关信息
步骤解析

第一步:创建文件对象
第二步:调用成员方法

🌰案例2— 获取当前文件对象的文件名


File file = new File("E:\\aba\\testaaa.txt");    //创建文件对象
file.createNewFile();							 //创建文件
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());

🗑️2.4 目录操作和文件删除

注意:【目录也是一种特殊形式存在的文件】

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

🌰案例3— 判断E盘aba目录下是否存在testaaa.txt,如果存在删掉

 String filepath = "E:\\aba\\testaaa.txt";    //定义好文件路径
        File file1 = new File(filepath);             //创建文件对象
        if (file1.exists()){                         //判断文件是否存在,再接着往下做
            if (file1.delete()){
                System.out.println(filepath+"删除成功!");
            }else {
                System.out.println(filepath+"删除失败!");
            }
        }else {
            System.out.println("文件不存在!");
        }

🌰案例4—判断e盘下atest目录是否存在,如果存在删除

String documentpath = "E:\\atest";    //定义好文件路径
        File file2 = new File(documentpath);             //创建文件对象
        if (file2.exists()){                         //判断文件是否存在,再接着往下做
            if (file2.delete()){
                System.out.println(documentpath+"删除成功!");
            }else {
                System.out.println(documentpath+"删除失败!");
            }
        }else {
            System.out.println("目录不存在!");
        }

📊3 javaIO流原理及分类

3.1 InputStream的常见子类

子类 说明
FileInputStream 文件字节流输入流
BufferedInputStreanm 缓冲输入流
ObjectInputStream 对象输入流

🌰案例5—FileInputStream读取文件数据

①int read() 从此输入流中读取一个字节的数据。当内容读取结束返回 -1;

    //读取E盘下aba目录下test03.txt文件内容
        String str = "E:\\aba\\test03.txt";			
        FileInputStream fileInputStream = new FileInputStream(str);
        int count =0;
        while ((count=fileInputStream.read()) != -1){
            System.out.print((char) count);
        }
        fileInputStream.close();

②int read(byte[] b) 从此输入流 b.length最多 b.length字节的数据读 b.length字节数组。最终读取结束后也会返回-1

 String str = "E:\\aba\\test03.txt";
        byte ch [] = new byte[8];
        int cout = 0;
        FileInputStream fileInputStream = new FileInputStream(str);
        while ((cout = fileInputStream.read(ch)) !=-1){
            String st = new String(ch,0,cout);
            System.out.print(st);
        }
        fileInputStream.close();

3.2 OutputStream的常见子类

  • FileOutputStream | 文件字节流输出流

🌰案例6— 写入文件案例

 String filePath = "E:\\aba\\write.txt";			//定义写入路径
 FileOutputStream fileOutputStream = new FileOutputStream(filePath);  //创建文件输出流对象
 fileOutputStream.write("hello,word".getBytes());   //通过write方法写入文件

注意:【此处创建对象时,在文件路径后加true,则写入数据追加在文件原内容之后,不加true这每次执行都会新写】

结语

大佬请留步javaIO流---看看这些关于文件的操作你都懂了么既然看到这了不如点个赞👍再走吧

本文目的在于分享技术以及在学习过程中个人记得需要注意的点,记录学习过程;
如果出现错误欢迎大家指正,如有意见或建议欢迎在评论区讨论

相关文章

暂无评论

暂无评论...