【Java】文件流

目录

一、文件流

二、 创建文件对象

 三、获取文件的相关信息

 四、目录操作和文件删除


一、文件流

  1. 流:数据从数据源(文件)和程序(内存)之间的路程
  2. 输入流:数据从数据源(文件)到程序(内存)的路径
  3. 输出流:数据从程序(内存)到数据源(文件)的路径

二、 创建文件对象

一个File对象代表一个文件

/**
 * 创建文件
 */
public class FileCreat {
    public static void main(String[] args) {

    }

    //1.new File(String pathname)//根据路径构建一个File对象
    @Test
    public void creat01() {
        String filePath = "D:\\aaaaa.txt";// 文件位置 + 文件名 \\ = /
        File file = new File(filePath);//在内存中创建了一个对象 (在程序中存在,并未写入到文件中)
        try {//createNewFile() 异常处理
            file.createNewFile();//执行createNewFile()后才会写入到对应的位置,变成一个文件
            System.out.println("创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //2.new File(File parent,String child)//根据父目录文件+子路径构建
    @Test
    public void creat02() {
        File parentfile = new File("D:\\");// 父目录对象位置 \\ = /
        String fileName = "bbbbb.txt";// 文件名
        File file = new File(parentfile, fileName);// (对象,字符串) (在程序中存在,并未写入到文件中)
        try {//createNewFile() 异常处理
            file.createNewFile();//执行createNewFile()后才会写入到对应的位置,变成一个文件
            System.out.println("创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //3.new File(String parent,String child) //根据父目录+子路径构建
    @Test
    public void creat03() {
        String parentPath = "D:\\";// 文件位置  (第一个\转义符)\\ = /
        String fileName = "ccccc.txt";// 文件名字
        File file = new File(parentPath, fileName);// 字符串,字符串 (在程序中存在,并未写入到文件中)
        try {//createNewFile() 异常处理
            file.createNewFile();//执行createNewFile()后才会写入到对应的位置,变成一个文件
            System.out.println("创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

 三、获取文件的相关信息

/**
 *一个为汉字 3个字节
 */
public class FileInformation {
    public static void main(String[] args) {
        //创建文件对象
        File file = new File("D:\\ddddd.txt");//文件

        System.out.println("文件名=" + file.getName());
        System.out.println("文件绝对路径=" + file.getAbsoluteFile());
        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());
    }
}

 四、目录操作和文件删除

/**
 * 文件、目录操作和文件、目录删除
 * java中目录也当作文件存在)
 */
public class Directory {
    public static void main(String[] args) {

    }

    //1.判断文件是否存在 如果存在就删除
    @Test
    public void f1() {
        File file = new File("D:\\ddddd.txt");//文件对象
        if (file.exists()) {//判断文件是否存在
            if (file.delete()) {//删除文件
                System.out.println("删除成功");
            }
        } else {
            System.out.println("文件不存在");
        }
    }

    //2.判断目录是否存在 如果存在就删除(java中目录也当作文件存在)
    @Test
    public void f2() {
        String filePath = "D:\\ddddd";//目录地址
        File file = new File(filePath);//目录对象
        if (file.exists()) {//判断目录是否存在
            if (file.delete()) {//删除目录
                System.out.println("删除成功");
            }
        } else {
            System.out.println("目录不存在");
        }
    }

    //3.判断目录(文件夹)是否存在,存在提示存在,不存在就创建
    @Test
    public void f3() {
        String directoryPath = "D:\\ddddd";//目录路径
        File file = new File(directoryPath);//目录对象
        if (file.exists()) {//判断目录是否存在
            if (file.delete()) {//删除目录
                System.out.println(directoryPath + "目录存在");
            }
        } else {
            System.out.println("目录不存在");
            file.mkdirs();//创建目录(文件夹)
            System.out.println("目录创建成功");
        }
    }

}
  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值