JavaIO编程——文件常用操作

创建文件操作

在JavaIO编程中,有以下三种创建文件的方式,分别是:

new File(String pathname)//根据路径构建一个File对象
new File(File parent,String child)//根据父目录文件+子路径构建
new File(String parent,String chile)//根据父目录+子路径构建

先看下面的这段程序,分别通过creat01,creat02,creat03三种方法来演示文件的创建:

package IOstream;

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

@SuppressWarnings({"all"})
/**
 * @Author Blueshadow
 * @Date 2021/9/7 20:36
 * @Version 1.0
 * 创建文件
 */
public class filesCreat {
    public static void main(String[] args) {
        filesCreat filesCreat = new filesCreat();
        filesCreat.creat01();
        filesCreat.creat02();
        filesCreat.creat03();
    }

    //方式一:根据文件路径来创建文件
    public void creat01(){
        String path = "E:\学习资料/编程/folder/news1.txt";//根据路径
        File file1 = new File(path);//还没有创建file对象
        try {
            file1.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式二:根据父目录文件+子路径构建
    public void creat02(){
        File pathName = new File("E:\学习资料/编程/folder");//父目录文件,即文件目录
        String fileName = "news2.txt";//子路径,即文件名
        File file1 = new File(pathName,fileName);
        try {
            file1.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式三:根据父目录+子路径构建
    public void creat03(){
        String parentPath = "E:\学习资料/编程/folder";//父目录
        String filePath = "/folder01/news1.txt";//文件路径
        File file = new File(parentPath,filePath);//创建文件对象
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
        }
    }
}

  • creat01:根据文件路径来创建文件。
public void creat01(){
        String path = "E:\学习资料/编程/folder/news1.txt";//定义了一个文件的路径
        File file1 = new File(path);//创建了一个File对象
        try {
            file1.createNewFile();//使用对象的createNewFile方法,根据定义好的路径去创建文件
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • creat02:根据父目录文件+子路径构建
public void creat02(){
        File pathName = new File("E:\学习资料/编程/folder");//父目录文件,即要创建文件所在的目录
        String fileName = "news2.txt";//子路径,即文件名
        File file1 = new File(pathName,fileName);//创建File对象
        try {
            file1.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • creat03:根据父目录文件+子路径构建。
public void creat03(){
        String parentPath = "E:\学习资料/编程/folder";//父目录
        String filePath = "/folder01/news1.txt";//文件路径
        File file = new File(parentPath,filePath);//创建文件对象
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
        }
    }

要使用以上方法去创建文件,只需要在主程序中去新建类对象,然后调用对应的方法即可。

filesCreat filesCreat = new filesCreat();
        filesCreat.creat01();
        filesCreat.creat02();
        filesCreat.creat03();

获取文件的相关信息

在JavaIO编程中,最常见的获取文件相关信息的方法有很多种,以下面程序为例:

package IOstream;

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

@SuppressWarnings({"all"})
/**
 * @Author Blueshadow
 * @Date 2021/7/28 9:02
 * @Version 1.0
 */
public class filesCreat {
    public static void main(String[] args) {
        filesCreat files = new filesCreat();
        files.info();
    }
    public void info(){
        //创建文件路径
        File file = new File("F:\test/news.txt");
        System.out.println(file.getName());//文件名称
        System.out.println(file.length());//文件大小(按照字节来计算)
        System.out.println(file.exists());//判断文件是否存在
        System.out.println(file.getAbsolutePath());//文件绝对路径
        System.out.println(file.getParent());//文件父级目录
        System.out.println(file.isFile());//判断是否是一个文件
        System.out.println(file.isDirectory());//判断是否是一个目录
    }
}

目录操作和文件删除

通过下面的程序来演示最常见的目录操作文件删除

package IOstream;

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

@SuppressWarnings({"all"})
/**
 * @Author Blueshadow
 * @Date 2021/7/28 9:02
 * @Version 1.0
 */
public class filesCreat {
    public static void main(String[] args) {
        filesCreat files = new filesCreat();
        files.m1();
        files.m2();

    }
    public void m1(){//判断文件是否存在,若是存在,则删除
        String filePath = "F:\test/news.txt";
        File file = new File(filePath);
        if (file.exists()){
            System.out.println("文件存在,进行删除");
            if (file.delete()){
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败!");
            }
        }else {
            System.out.println("文件不存在");
        }

    }

    public void m2(){//判断目录是否存在,若是存在,则删除
        String filePath = "F:\test";
        File file = new File(filePath);
        if (file.exists()){
            System.out.println("该目录存在,进行删除");
            if (file.delete()){
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败!");
            }
        }else {
            System.out.println("目录不存在");
        }

    }
}

原文地址:https://www.cnblogs.com/nanfengashuai/p/15240275.html