创建文件的三种方式和一些操作方法

package IO;

import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;

/**
 * @author 小小小白白白白
 */
public class Create {
    public static void main(String[] args) {}

    /**
     * 创建文件的三种方式
     * 1 new File(String pathname)  //根据路径构建一个File对象
     * 2 new File(File parent,String child)  //根据父目录文件+子路径构建
     * 3 new File(String parent,String child)  //根据父目录+子路径构建
     * createNewFile  //创建新文件
     */
    //方式1
    @Test
    public void file1(){
        //指定文件的创建路径
        String filepath = "e:\\new1.txt";
        //生成创建对象
        File file = new File(filepath);
        //调用创建方法
        try {
            file.createNewFile();
            System.out.println("文件1创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    //方式2
    public void file2(){
        //父目录路径
        File parentFile = new File("e:\\");
        //子文件名
        String filename="new2.txt";
        //这里并没有创建文件只是在内存中生成了
        File file = new File(parentFile, filename);

        try {
            //这里才是创建文件将内存中生成的写入磁盘中
            file.createNewFile();
            System.out.println("文件2创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式3
    @Test
    public void file3(){
        String parentPath="e:\\";
        String filename="new3.txt";
        File file=new File(parentPath,filename);
        try {
            file.createNewFile();
            System.out.println("文件3创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    //一些文件方法
    @Test
    public void info(){
        //先创建文件对象
        File file = new File("e:\\new1.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());

        //是否是一个文件
        System.out.println("是否是一个文件"+file.isFile());

        //是否是一个目录
        System.out.println("是否是一个目录="+file.isDirectory());
    }

    //对文件(目录)进行操作
    @Test
    public void delete(){
        String filepath = "e:\\new1.txt";

        //在Java中目录也可以被当做文件进行操作
        String filepath1 = "e:\\111";

        //对多级目录进行操作
        String filepath2 = "e:\\Test\\a\\b\\c";
        File file = new File(filepath1);
        File file1 = new File(filepath2);
        if(file.exists()){
            if(file.delete()){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        }else {
            System.out.println("文件不存在");
        }

        if(file1.exists()){
            System.out.println("目录存在");
        }else {
            //这里如果是mkdirs就是能创建多级目录,如果是mkdir则只能创建一个目录
            if(file1.mkdirs()){
                System.out.println("创建成功");
            }else {
                System.out.println("创建失败");
            }
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值