Java重修笔记 第五十八天 坦克大战(八)IO 流

  • 什么是文件

        文件就是保存数据的地方,既可以保存视频,也可以保存声音、图片等等

  • 什么是文件流

        文件在程序中是以流的形式来操作的

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

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

  • 文件类的构造方法

1. public File(URI uri)

        URI转换为抽象路径名来创建新的File实例

        参数:uri - 文件对象的绝对路径

2. public File(File parent, String child)

        父文件类和子路径名字符串创建新的File实例

        参数:parent - 父抽象路径名 child - 子路径名字符串

3. public File(String parent, String child)

        父路径名字符串和子路径名字符串创建新的File实例

        参数:parent - 父路径名字符串 child - 子路径名字符串

public class FileCreate {
    public static void main(String[] args) {

    }

    // 方法一:
    @Test
    public void create01() {
        String filePath = "d:\\new1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 方法二:
    @Test
    public void create02() {
        File parentFile = new File("d:\\");
        String fileName = "new2.txt";
        File file = new File(parentFile, fileName);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

  • 文件类 File 的常用方法
1. 获取文件名

        public String getName()

        返回该文件类指向文件的文件名

2. 获取文件的绝对路径

        public String getAbsolutePath()

        返回文件的绝对路径

3. 获取该文件的父目录

        public File getParentFile()

        返回该文件的父级目录

4. 获取文件的字节大小

        public long length()

        返回文件所占的字节数

5. 判断文件是否存在

        public boolean exists()

        判断此路径名表示的文件或目录是否存在,若存在就返回 true

6. 判断是否为文件

        public boolean isFile()

        如果是一个文件则返回 true 

7. 判断是否为一个目录

        public boolean isDirectory()

        如果是一个目录则返回 true

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

    }
8. 删除目录或文件

        public boolean delete()

        删除成功返回 true ,如果删除的是目录的话,该目录下必须为空

9. 创建单级目录

        public boolean mkdir()

        在该路径下创建相应单级目录

10. 创建多级目录

        public boolean mkdirs()

        在该路径下创建相应多级目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值