Day22-C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\com\File-FileTest1~4

File

  1. 文件是非常重要的存储方式,在计算机硬盘中
  2. 即便断电或者程序终止,存储在硬盘中的文件也不会丢失
  3. 是java.io包下的类,File类的对象,用于代表当前操作系统的文件(可以是文件或者文件夹)
  4. 获取文件信息(大小,文件名,修改时间)、判断文件类型、创建文件/文件夹、删除文件/文件夹...
  5. File类只能对文件本身进行操作,并不能读写文件里面存储的数据

image-20251023144615485

package com.File;

import java.io.File;

public class FileTest1 {
    public static void main(String[] args) {
        //1、创建一个File对象,指代某个具体的文件
        //路径分割符
        //File f1 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\新建 文本文档.txt");
        //File f1 = new File("C:/Users/Lenovo/Desktop/note/code/新建 文本文档.txt");(推荐)
        File f1 = new File("C:"+File.separator+ "Users"+File.separator+ "Lenovo/Desktop/note/code/新建 文本文档.txt");
        System.out.println(f1.length());

        File f2 = new File("C:/Users/Lenovo/Desktop/note/code");
        System.out.println(f2.length());//4096
        //指的是文件夹本身的大小,不包含里面的文件内容

        File f3 = new File("C:/Users/Lenovo/Desktop/note/code/666.txt");
        System.out.println(f3.length());
        System.out.println(f3.exists());//false

        //我现在要定位的文件在模块中,应该怎么定位呢?
        //绝对路径,带盘符的
        //File f4 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\JavaSE\\Basic\\src\\itheima.txt");

        //相对路径(重点),不带盘符,默认是直接去工程下寻找文件的,对应左侧上方那个黄色标的JavaSE
        File f4 = new File("Basic\\src\\itheima.txt");
        System.out.println(f4.length());
    }
}
package com.File;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileTest2 {
    public static void main(String[] args) {
        //1、创建文件对象,指代某个文件
        File f1 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\ab.txt");

        //2、public boolean exists():判断当前文件对象,对应文件的路径是否存在,存在返回true
        System.out.println(f1.exists());

        //3、判断当前文件对象指代的是否是文件,是文件返回true,反之
        System.out.println(f1.isFile());

        //4、判断当前文件对象指代的是否是文件夹,是文件夹返回true,反之
        System.out.println(f1.isDirectory());//false

        //5、获取文件的名称(包含后缀)
        System.out.println(f1.getName());

        //6、获取文件的大小,返回字节个数
        System.out.println(f1.length());

        //7、获取文件的最后修改时间
        long time = f1.lastModified();
        //String sdf = new SimpleDateFormat().format(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(time));

        //8、获取创建文件对象时使用的路径
        File f2 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\ab.txt");
        File f3 = new File("Basic\\src\\itheima.txt");
        System.out.println(f2.getPath());//C:\Users\Lenovo\Desktop\note\code\ab.txt
        System.out.println(f3.getPath());//Basic\src\itheima.txt

        //9、获取绝对路径
        System.out.println(f2.getAbsolutePath());//C:\Users\Lenovo\Desktop\note\code\ab.txt
        System.out.println(f3.getAbsolutePath());//C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\itheima.txt
    }
}
package com.File;

import java.io.File;
import java.io.IOException;

public class FileTest3 {
    public static void main(String[] args) throws IOException {
        //1、创建一个新文件(内容为空),创建成功则返回true,反之
        File f1 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\itheima2.txt");
        System.out.println(f1.createNewFile());//alt+Enter直接抛出异常
        //只能创建一次,运行第二次程序会返回false

        //2、用于创建文件夹,注意:只能创建一级文件夹
        File f2 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\aaa");
        System.out.println(f2.mkdir());

        //3、用于创建文件夹,注意:可以创建多级文件夹
        File f3 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\aaa\\bbb\\ccc");
        System.out.println(f3.mkdirs());

        //4、删除文件、或者空文件,注意:不能删除空文件夹
        System.out.println(f1.delete());
        System.out.println(f2.delete());//f2是非空文件夹,不能删除//false
        System.out.println(f3.delete());
    }
}

image-20251023154335459

package com.File;

import java.io.File;
import java.lang.reflect.Array;
import java.util.Arrays;


public class FileTest4 {
    public static void main(String[] args) {
        //1、获取当前目录下所有的“一级文件名称”到一个字符串数组中去返回
        File f1 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code");
        String[] names = f1.list();
        for (String name : names) {
            System.out.println(name);
        }

        //2、(重点)获取当前目录下的所有的“一级文件对象”到一个对象数组中去返回(重点)
        File[] files = f1.listFiles();
        for (File file : files) {
            System.out.println(file.getAbsolutePath());
        }

        File f = new File("C:\\Users\\Lenovos");//不存在的文件夹
        File[] files1 = f.listFiles();
        System.out.println(files1);//null
        File f2 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\ab.txt");//这个是文件不是文件夹
        File[] files2 = f2.listFiles();
        System.out.println(files2);
        File f3 = new File("C:\\Users\\Lenovo\\Desktop\\note\\code\\aaa\\bbb");
        File[] files3 = f3.listFiles();
        System.out.println(Arrays.toString(files3));//[]因为主调f3是空文件夹

    }
}

IO流

用于读写数据(可以读写文件或网络中的数据)

posted @ 2025-10-23 17:15  David大胃  阅读(8)  评论(0)    收藏  举报