p1 文件的基本使用

发布时间 2023-06-04 10:10:57作者: 凉白茶

文件的基本使用

一、文件

  • 什么是文件

    文件是保存数据的地方,比如word文档,txt文件,excel文件……都是文件。即可以保存一张图片,也可以保持视频,声音……

  • 文件流

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

    文件流

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

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

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

二、常用的文件操作

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

  1. new File(String pathName) :根据路径构建一个File对象,类似绝对路径
  2. new File(File parent, String child):根据父目录文件夹和子路径构建,类似根据相对路径
  3. new File(String parent, String child):根据父目录 和 子路径构建
  4. createNewFile(): 创建新文件
  • 代码演示:

    import org.junit.jupiter.api.Test;
    
    import java.io.File;
    import java.io.IOException;
    
    
    /**
     * @author
     * @version 1.0
     * 演示创建文件
     */
    public class FileCreate {
        public static void main(String[] args) {
    
        }
    
        //方式1 new File(String pathName),类似根据绝对路径创建?
        @Test
        public void create01(){
            String pathName = "e:\\news1.txt";
            File file = new File(pathName);//创建文件对象,此时只是有一个对象在jvm内存中
            try {
                file.createNewFile();//创建文件,这里才对磁盘做出操作,创建出文件
                System.out.println("文件创建成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //方式2 new File(File parent, String child),类似根据相对路径创建?
        //根据父目录文件夹和子路径构建
        @Test
        public void create02(){
            File parentFile = new File("e:\\");
            String fileName = "news2.txt";
            File file = new File(parentFile, fileName);//创建文件对象
    
            try {
                file.createNewFile();//创建文件
                System.out.println("文件创建成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //方式3 new File(String parent, String child),根据父目录 + 子路径构建
        @Test
        public void create03(){
    //        String parentPath = "e:\\";
            String parentPath = "e:/";//也可以这么写
            String fileName = "news3.txt";
            File file = new File(parentPath, fileName);
            try {
                file.createNewFile();
                System.out.println("文件创建成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    

2. 获取文件的相关信息的方法

  • getName(),getAbsolutePath(),getParent(),length(),exists(),isFile(),isDirectory()

  • 代码演示:

    import org.junit.jupiter.api.Test;
    
    import java.io.File;
    import java.io.IOException;
    
    public class FileInformation {
        public static void main(String[] args) {
    
        }
        //获取文件信息
        @Test
        public void info(){
            //创建文件对象
            File file = new File("e:\\news1.txt");//File对象只是一个路径,可能是文件也可能是目录(文件夹)
    //        try {
    //            file.createNewFile();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
            //获取文件名字
            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());
        }
    }
    
    /*
    运行结果:
    文件名字 = news1.txt
    文件绝对路径 = e:\news1.txt
    文件父目录 = e:\
    文件大小 = 18
    文件是否存在 = true
    是不是一个文件 = true
    是不是一个目录 = false
    */
    

3. 目录的操作和文件删除

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

代码演示:

import org.junit.jupiter.api.Test;

import java.io.File;

public class Directory_ {
    public static void main(String[] args) {

    }
    //判断 e:\\news1.txt 是否存在,存在就删除
    @Test
    public void m1(){
        String filePath = "e:\\news1.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("文件不存在……");
        }
    }

    //判断 D:\\demo02 是否存在,存在就删除
    //目录也是文件
    @Test
    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(filePath + " 删除失败");
            }
        }else {
            System.out.println("该目录不存在……");
        }
    }

    //判断D:\\demo\\a\\b\\c 目录是否存在,如果存在就提示已经存在,否则就创建
    @Test
    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()){//创建一级目录用mkdir(),创建多级目录使用mkdirs()
                System.out.println(directoryPath + "创建成功...");
            }else{
                System.out.println(directoryPath + "创建失败...");
            }
        }
    }

}