Day30-文件基础知识、创建文件、获取文件信息、目录操作

Day30-文件基础知识、创建文件、获取文件信息、目录操作

在这里插入图片描述

文件基础知识

  • 什么是文件

文件是保存数据的地方。

  • 文件流

在这里插入图片描述

创建文件

常用的文件操作

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

在这里插入图片描述

  • 案列

方式一 :new File(String pathname)

package com.ghy.file;

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

//演示创建文件
//方式一 new File(String pathname)
public class FileCreate01 {
    public static void main(String[] args) {
        String filePath ="d:\\code\\news1.txt";//路径
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件1创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

输出

文件1创建成功

查看

在这里插入图片描述

方式二:new File(File parent,String child)//根据父目录文件+子路径构建

package com.ghy.file;

import jdk.nashorn.internal.ir.CallNode;

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

//方式二:new File(File parent,String child)//根据父目录文件+子路径构建
public class FileCreate02 {
    public static void main(String[] args) {
        File parentfile = new File("d:\\code\\");//路径
        String fileName = "news2.txt";//文件名和文件类型
        //这里的file对象,只是java程序中的一个对象
        //只有执行了file.createNewFile();才会真正在磁盘中创建该文件
        File file = new File(parentfile, fileName);
        //执行方法
        try {
            file.createNewFile();
            System.out.println("文件2创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输出

文件2创建成功

查看

在这里插入图片描述

方式三:new File(String parent,String child)//根据父目录+子路径构建

package com.ghy.file;
import java.io.File;
import java.io.IOException;
//方式三:new File(String parent,String child)//根据父目录+子路径构建
public class FileCreate03 {
    public static void main(String[] args) {
        String parentFile = "d:\\code\\";
        String fileName = "news3.txt";
        File file = new File(parentFile,fileName);

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

输出

文件3创建成功

查看

在这里插入图片描述

方法二和方法三的区别:一个是对象,一个是String类型的。

获取文件信息

在这里插入图片描述

package com.ghy.file;
import java.io.File;
public class FileInformation {
    public static void main(String[] args) {
        //创建文件对象
        File file = new File("d:\\code\\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());//T
        System.out.println("是不是一个文件:"+file.isFile());//T
        System.out.println("是不是一个目录:"+file.isDirectory());//F
    }
}

输出

文件名是:news1.txt
文件绝对路径:d:\code\news1.txt
文件父目录:d:\code
文件大小(字节):0
文件是否存在:true
是不是一个文件:true
是不是一个目录:false

目录操作

在这里插入图片描述

package com.ghy.file;

import java.io.File;

public class FileDirectory {
    public static void main(String[] args) {
        //判断 d:\\code\\news1.txt 是否存在,如果存在就删除
        String filePath = "d:\\code\\news1.txt";//路径
        File file = new File(filePath);
        if(file.exists()){//判断文件是否存在
            if(file.delete()){//file.delete()返回的值是布尔类型,成功就输出删除成功,失败就失败
                System.out.println(filePath+"删除成功!");
            }else{
                System.out.println(filePath+"删除失败!");
            }
        }else{
            System.out.println("该文件不存在");
        }
    }
}

输出

d:\code\news1.txt删除成功!

查看

在这里插入图片描述

目录也是特殊的文件,该方法也适用于目录。

package com.ghy.file;

import java.io.File;
public class FileDirectory02 {
    public static void main(String[] args) {
        //判断 d:\\code\\news1\\a\\b\\c 是否存在,如果不存在就创建
        String directoryPath = "d:\\code\\a\\b\\c";//路径
        File file = new File(directoryPath);
        if(file.exists()){//判断文件是否存在
            System.out.println(directoryPath+"文件存在");
            }else{
                if(file.mkdirs()){//file.mkdirs()也是返回布尔类型,判断是否创建成功
                    System.out.println(directoryPath+"创建成功!");
                }else{
                    System.out.println(directoryPath+"创建失败!");
                }
            }
    }
}

输出

d:\code\a\b\c创建成功!

查看

在这里插入图片描述

*** 创建一级目录file.mkdir(),创

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值