java中file文件的操作

骑士李四记录
在java中对文件的操作也是很重要的一块,我们这里来复习总结一下文件操作的基本使用

1.提纲:

1.文件的创建
2.文件的删除
3.文件的查看
4.文件的获取
5.文件的读写

2.代码:

package com.pengli.programutils.utils.javabase;

import java.io.*;

/**
 * @Description: 文件的操作
 * @Author: pengli
 * @CreateDate: 2020/5/24 10:31
 */
public class TestFile {
    /***
     * File 既可以表示文件,也可以目录
     * 可以访问其表示的文件或者目录的属性,
     * 可以创建,删除一个文件,可以访问其子项的信息
     * 但是不能访问文件的内容
     */
    public static void main(String args[]) throws IOException {
        /**
         * 查看文件信息
         */
        //separator当前系统的相对路径,linux和win系统的杠就表示一样,
        File file1 = new File("."+File.separator+"demo.txt");
        File file = new File("./demo.txt");//当前目录,项目的根目录

        String fileName = file.getName();
        long length = file.length();//文件的字节数
        System.out.println(length);

        //可读
        boolean canRead = file.canRead();

        //可写
        boolean canWrite =file.canWrite();

        //影藏
        boolean isHidden = file.isHidden();
        System.out.println(isHidden);

        /**
         * 创建文件
         * win默认是根目录,  "./" "."+File.separator+"/文件名",也是当前项目根目录的相对路径
         */
        File file2 = new File("test.txt");
        if (!file2.exists()){
            file2.createNewFile();
            System.out.println("创建完毕");
        }else{
            System.out.println("已存在");
        }

        /**
         * 删除文件
         */
        if (file2.exists()) {
            file2.delete();
            System.out.println("删除完毕");
        }else{
            System.out.println("文件不存在");
        }

        /**
         * 创建目录
         */
        File dir = new File("demo1");
        if (!dir.exists()) {
            dir.mkdir();
            System.out.println("创建目录完毕");
        }else{
            System.out.println("目录已存在");
        }

        /**
         * 创建 当前目录的同时将所有不存在的父目录一起创建起来
         */
        File dirs = new File("a"+File.separator+"b"+File.separator+"c");
        if (!dirs.exists()) {
            dirs.mkdirs();
            System.out.println("创建目录完毕");
        }else{
            System.out.println("已存在");
        }

        /**
         * 删除目录 删除的目录下面必须要没有内容,是空目录
         */
        File dir1 = new File("demo1");
        if (dir1.exists()){
            dir.delete();
            System.out.print("删除成功");
        }

        /**
         * 删除指定目录
         */

        File file4 = new File("a");
        delete(file4);

        /**
         * 获取一个目录的所有子项
         * boolean isFile()      是否是文件
         * boolean isDirectory() 是否是目录
         * File[] listFiles()    获取所有的子项
         */
        File dir2 = new File(".");
        if (dir.isDirectory()) {
            File[] subs = dir2.listFiles();
            for  (File file3 :subs) {
                if (file3.isFile()){
                    System.out.print("文件");
                }else {
                    System.out.println("目录");
                }
            }
        }

        /**
         * 获取名字以点开头的子项 (获取指定条件的文件)
         */
        File dir3 = new File(".");
        File[] subs = dir3.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                System.out.println("正在过滤:"+file.getName());
                return file.getName().startsWith(".");
            }
        });

        for (File sub :subs){
            System.out.println(sub.getName());
        }



        /***
         * RandomAccessFile 的使用
         * r:只读模式
         * rw:读写模式
         * 自动判断文件时候存在,不存在,自动创建文件,但是不创建目录,如果没有目录,就我们自己创建目录在再去,,
         */
        RandomAccessFile randomAccessFile = new RandomAccessFile("raf.dat","rw");
        /*
         * void write(int i)
         * 写出给定的int值对应的2进制的低八位
         */
        randomAccessFile.write(97);
        randomAccessFile.write(1);
        /*
         * int read()
         * 读取一个字节,并以10进制的int返回,
         * 若返回值为-1,则表示读到了文件末尾
         */
        int d = randomAccessFile.read();
        System.out.println(d);
        randomAccessFile.close();

        /**
         * 从一个文件读取到另一个文件
         */
        copyFromRda1ToRda2();

        /**
         * 提高读取效率 通过提高每次的读取量
         */
        copyFromRda3ToRda4Fast();

        /**
         * RandomAccessFile:提供读取基本数据类型的方法
         */
        randomAccessFileToDataType();


    }


    /**
     * RandomAccessFile:提供读取基本数据类型的方法
     * writeInt()读完之后,指针在读入数据的最后一位,所以不能直接用户readInt();要把指针
     * 挪到开始的位置,才能开始连续读。不想连读读,也可以跳指针读。
     */
    public static void randomAccessFileToDataType() throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("rda3.txt","rw");
        int max = Integer.MAX_VALUE;
        randomAccessFile.writeInt(max); //一个字节一个字节的读,就是8位8位的往后读
        randomAccessFile.writeLong(1234L);
        randomAccessFile.writeDouble(123.123);
        /*
          void seek(long pos)   移动指针到指定位置
         */
        randomAccessFile.seek(0); //移动到开始0的位置
        System.out.println("pos:"+randomAccessFile.getFilePointer()); //获取当前位置
        int i = randomAccessFile.readInt();
        System.out.println("pos:"+randomAccessFile.getFilePointer()); //获取当前位置
        long l = randomAccessFile.readLong();
        System.out.println("pos:"+randomAccessFile.getFilePointer()); //获取当前位置

        double d = randomAccessFile.readDouble();
        System.out.println("pos:"+randomAccessFile.getFilePointer()); //获取当前位置

    }

    /**
     * 提高读取效率读取 通过提高每次的读取量
     */
    public static void copyFromRda3ToRda4Fast() throws IOException {
        /*
         * int read(byte[] data);
         * 一次性尝试读取给定的字节数组总长度的字节量并存入该数组中,
         * 返回值为实际读取的字节量,若返回值为-1,则表示没有读取到任何数据(文件末尾)
         *
         * void write(byte[] data)
         * 一次性把给的所有字节全部读出去
         *
         * void write(byte[] data ,0,len)
         * 将给定字节从0到len的字节连续写入
         *
         */
        RandomAccessFile rda3 = new RandomAccessFile("rda3.txt","rw");
        RandomAccessFile rda4 = new RandomAccessFile("rda4.txt","rw");
        //10k
         byte[] bytes = new byte[1024*10];
         int len = -1;
         long start = System.currentTimeMillis();
         while ((len = rda3.read(bytes)) != -1){
             rda4.write(bytes,0,len);
         }
         long end = System.currentTimeMillis();
         System.out.print(end-start);
         rda3.close();
         rda4.close();
    }

    /**
     * 从一个文件读取到另一个文件
     */
    public static void copyFromRda1ToRda2() throws IOException {
        RandomAccessFile rda1 = new RandomAccessFile("rda1.txt","rw");
        RandomAccessFile rda2 = new RandomAccessFile("rda2.txt","rw");

        //保存读取的每个字节
        int d = -1;
        long start = System.currentTimeMillis();
        while ((d=rda1.read()) != -1){
            rda2.write(d);
        }
        long end = System.currentTimeMillis();
        System.out.println("读取完毕:"+ (end - start));
        rda1.close();
        rda2.close();
    }

    /**
     * 删除指定目录下所有文件和目录
     */
    public static void delete(File file){
       if (file.isDirectory()){
           for (File file1 : file.listFiles()){
               //整个流程走到指定位置时候,需要再走一遍的时候,就是递归。
                delete(file1);
           }
       }
       file.delete();
    }
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值