Java中文件的创建(三种方式),文件常用的方法

文件的创建

  1. 方式1: new File(String pathName) 根据路径构建一个File对象
  2. 方式2: new File(File parent,String child) 根据父目录文件+子路径构建
  3. 方式3: new File(String parent,String child) 根据父目录+子路径构建

代码:

//方式1 new File(String pathName) 根据路径构建一个File对象
    public void creat1() {
        String pathName = "d:\\creatFile\\file1.txt";
        File file1 = new File(pathName);
        try {
            file1.createNewFile();
            System.out.println("文件1创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式2 new File(File parent,String child) 根据父目录文件+子路径构建
    public void creat2(){
        File parent = new File("d:\\");
        String child = "file2.txt";
        File file2 = new File(parent, child);
        try {
            file2.createNewFile();
            System.out.println("文件2创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式3 new File(String parent,String child) 根据父目录+子路径构建
    public void creat3(){
        String parent = "d:\\";
        String child = "file3.txt";
        File file2 = new File(parent, child);
        try {
            file2.createNewFile();
            System.out.println("文件3创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

常用方法

  1. getName():获取文件名字。
  2. getAbsolutePath():获取文件的绝对路径。
  3. getParent():获取文件的父级目录。
  4. length():获取文件的大小(以字节为单位)。
  5. exists():判断文件是否存在。
  6. isFile():判断是否为一个文件。
  7. isDirectory():判断是否为一个目录。
  8. delete():删除文件
  9. mkdirs():创建多级目录
  10. mkdir():创建父目录(一级目录)

代码:

public void fileMethods(){
        //先创建文件对象
        File file = new File("d:\\file1.txt");
        //调用相应的方法,得到对应信息
        System.out.println("文件名字=" + file.getName());
        //getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
        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
    }
public void delete(){
        File file1 = new File("d:\\file3.txt");
        if(!file1.exists()){
            System.out.println("文件不存在");
        }else {
            if(file1.delete()){
                System.out.println("文件删除成功");
            }else {
                System.out.println("文件删除失败");
            }
        }
    }

    public void delete2(){
        File file1 = new File("d:\\file1");
        if(!file1.exists()){
            System.out.println("文件不存在");
        }else {
            if(file1.delete()){
                System.out.println("文件删除成功");
            }else {
                System.out.println("文件删除失败");
            }
        }
    }

    public void creat(){
        String directoryPath = "d:\\file1\\ret";
        File file = new File(directoryPath);
        if(file.exists()){
            System.out.println("文件存在");
        }else {
            if(file.mkdirs()){//mkdirs()创建多级目录,如:d:\\file1\\ret。mkdir()创建父一级目录,如d:\\file2
                System.out.println("文件创建成功");
            }else {
                System.out.println("文件创建失败");
            }
        }
    }
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java创建文件方式主要有以下三种: 1. 使用File类的createNewFile()方法创建文件 ```java import java.io.File; import java.io.IOException; public class CreateFileExample { public static void main(String[] args) { try { // 指定文件路径和名称 String filePath = "C:/example/newfile.txt"; // 创建File对象 File file = new File(filePath); // 创建文件 boolean result = file.createNewFile(); if (result) { System.out.println("文件创建成功!"); } else { System.out.println("文件已存在!"); } } catch (IOException e) { e.printStackTrace(); } } } ``` 2. 使用FileOutputStream类创建文件 ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class CreateFileExample { public static void main(String[] args) { FileOutputStream fos = null; try { // 指定文件路径和名称 String filePath = "C:/example/newfile.txt"; // 创建File对象 File file = new File(filePath); // 创建输出流 fos = new FileOutputStream(file); System.out.println("文件创建成功!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } } ``` 3. 使用BufferedWriter类创建文件 ```java import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class CreateFileExample { public static void main(String[] args) { BufferedWriter bw = null; try { // 指定文件路径和名称 String filePath = "C:/example/newfile.txt"; // 创建File对象 File file = new File(filePath); // 创建输出流 bw = new BufferedWriter(new FileWriter(file)); System.out.println("文件创建成功!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } } } } ``` 以上三种方式都可以用来创建文件。第一种方式使用了File类的createNewFile()方法,第二种方式使用了FileOutputStream类,第三种方式使用了BufferedWriter类。无论使用哪种方式,都需要先创建File对象,并指定要创建文件路径和名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值