IO流——文件基础

IO流——文件基础

文件介绍

文件就是保存数据的地方

输入和输出是以 Java程序(内存)为参考

在这里插入图片描述

文件的创建【3 种方式】

  1. new File(String path) // 根据路径构建一个File对象
  2. new File(File parentFile,String childPath) // 根据父目录文件 + 子路径构建
  3. new File(String parentPath,String childPath) // 根据父目录 + 子路径构建
  4. 注意:以上三种方式,只是在 Java 程序里创建了对象,还需执行 file.createNewFile() 方法,才会真正地在磁盘里创建好文件!

package day06.FileCreate;

import org.junit.Test;

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

/**
 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 10:41 2021/9/2
 */
public class Demo1 {
    public static void main(String[] args) {

    }

    // 方式 1:new File(String path);
    @Test
    public void create1(){
        String path = "E:\\news1.txt";
        File file = new File(path);
        try {
            file.createNewFile();
            System.out.println("文件创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    // 方式 2:new File(File parentFile, String childPath);
    @Test
    public void create2() {
        File parentFile = new File("E:\\");
        String childPath = "news2.txt";
        File file = new File(parentFile, childPath);

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

    // 方式 3:new File(String parenPath, String childPath);
    @Test
    public void create3() {
        String parentPath = "E:\\";
        String childPath = "news3.txt";
        File file = new File(parentPath, childPath);

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

}


执行结果:

在这里插入图片描述

获取文件的信息

常用方法

getName【获取文件名】、getAbsolutePath【获取绝对路径】、getParent【获取父级路径】、length【获取文件大小】、exists【判断是否文件存在】、isFile【判断是否是一个文件】、isDirector【判断是否是一个目录】


package day06.getFileInfo;

import org.junit.Test;

import java.io.File;

/**
 * @Author: Gin
 * @Description:
 * @Modified By: Gin
 * @Date: Created in 10:58 2021/9/2
 */
public class GetFileInformation {
    public static void main(String[] args) {

    }

    @Test
    public void getInfo(){
        // 先创建文件对象
        // news1.txt 中的内容为:hello刘德华
        // 编码为 UTF-8,则一个英文字母占 1 个字节,一个汉字占 3 个字节,共占 14 个字节
        File file = new File("E:\\news1.txt");

        // 调用响应方法,得到对应文件信息
        // getName、getAbsolutePath、getParent、length、exists、isFile、isDirector
        // 1. getName
        System.out.println(file.getName());
        // 2. getAbsolutePath
        System.out.println(file.getAbsolutePath());
        // 3. getParent
        System.out.println(file.getParent());
        // 4. length
        System.out.println(file.length());
        // 5. exists
        System.out.println(file.exists());
        // 6. isFile
        System.out.println(file.isFile());
        // 7. isDirector
        System.out.println(file.isDirectory());

    }

}


mkdir【创建一级目录】、mkdirs【创建多级目录】、delete【删除文件或空目录】



    // file.delete() 删除文件
    @Test
    public void m1(){
        String filePath = "E:\\news1.txt";
        File file = new File(filePath);

        if(file.exists()){
            if(file.delete()){
                System.out.println(filePath + " 删除成功!");
            }else{
                System.out.println("删除失败 !");
            }
        }else{
            System.out.println("文件不存在!");
        }
    }

    // file.delete() 删除目录
    @Test
    public void m2(){
        String directorPath = "E:\\DeleteDemo";
        File file = new File(directorPath);

        if(file.exists()){
            if(file.delete()){
                System.out.println(directorPath + " 删除成功!");
            }else{
                System.out.println("删除失败 !");
            }
        }else{
            System.out.println("目录不存在!");
        }

    }

    // file.mkdirs() 创建多级目录
    // 判断 E:\\a\\b\\c\\d 是否存在,如果存在就提示已存在,不存在就创建
    @Test
    public void m3(){
        String directorPath = "E:\\a\\b\\c\\d\\e";
        File file = new File(directorPath);

        if(file.exists()){
            System.out.println("文件 " + directorPath + " 已存在!");
        }else{
            if(file.mkdirs()){
                System.out.println(directorPath + " 创建成功!");
            }else{
                System.out.println(directorPath + " 创建失败!");
            }
        }

    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值