简单聊一下File类

1.什么是File类?

这是java的一个对文件进行增删改查的类

2.简单使用1:创建文件和删除文件

案例1:创建文件,判断是否存在并写入数据。
  public static void main(String[] args) throws IOException {
        File file=null;
        FileOutputStream fos=null;
        try {
            file=new File("F:/text.txt");
            //如果文件不存在,则创建文件
            if(!file.exists()){
                boolean flag = file.createNewFile();
                System.out.println("文件是否创建成功:"+flag);
            }
            System.out.println("文件的名字:"+file.getName());
            System.out.println("文件的绝对路径:"+file.getAbsolutePath());
            System.out.println("文件的大小:"+file.length());//目前应该为0
            //往文件写入信息,再次查看文件大小
            fos=new FileOutputStream(file);
            fos.write(97);//a
            fos.write(98);//b
            System.out.println("文件的大小:"+file.length());//目前应该为2,一个字符一个字节
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if(fos!=null){
                fos.close();
            }
        }
    }

案例2:删除文件
 public static void main(String[] args) {
        File file=null;
        try {
            file=new File("F:/text.txt");
            //如果文件存在则删除
            if(file.exists()){
                boolean delete = file.delete();
                System.out.println("文件是否删除成功:"+delete);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
文件是否删除成功:true

3..查询文件

案例1:查询输入路径下的文件

键盘输入文件路径,输出该路径下的所有文件

    public static void main(String[] args) throws Exception{
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您想查询的文件路径");
        String url = sc.nextLine();
        File file=new File(url);
        getFiles(file);

    }

    private static void getFiles(File file) {
       if(file.isFile()){
            //如果是文件的话就打印文件名字
            System.out.println(file.getName());
        }else if(file.isDirectory()){
            //如果是文件夹则使用递归继续遍历
            File[] files = file.listFiles();
            for (File file1 : files) {
                getFiles(file1);
            }
        }
    }

这里放出我的java代码文件目录

案例2:键盘输入路径后,输出该路径下后缀是.webp的文件
  public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您想查询的文件路径");
        String url = sc.nextLine();
        File file = new File(url);
        selectWebpFile(file);
    }

    private static void selectWebpFile(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                //匹配后缀名称
                if (file1.getName().endsWith(".webp")) {
                    System.out.println(file1.getAbsolutePath());
                }else {
                    selectWebpFile(file1);
                }
            }
        } else {
            if (file.getName().endsWith(".webp")) {
                System.out.println(file.getAbsolutePath());
            }
        }
    }

4简单使用:拷贝文件

案例1:简单拷贝指定路径下的文件
 public static void main(String[] args) throws IOException {
        //将F:/a.txt复制到F:/aa/a.txt
        File file=null;
        File file1=null;
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            file=new File("F:/a.txt");
            file1=new File("F:aa","a.txt");
            //读取a.txt文件,逐渐写入F:/aa/a.txt文件中
            int len;
            byte[] bytes=new byte[8192];
            fis=new FileInputStream(file);
            fos=new FileOutputStream(file1);
            while ((len=fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if (fos!=null){
                fos.close();
            }
            if(fis!=null){
                fis.close();
            }
        }
    }
案例2:将F:/aa/a.txt复制到E:/aa/a.txt,不复制内容
  public static void main(String[] args) throws Exception{
        File file=new File("F:/aa/a.txt");
        deepCopyFile(file);
       
    }

    private static void deepCopyFile(File file) throws IOException {
        String parent = file.getParent().substring(3);//aa
        File newFile=new File("E:/"+parent);
        //先创建文件夹,不然会报错
        newFile.mkdir();
        newFile=new File(newFile.getAbsolutePath(),file.getName());
        //创建文件
        newFile.createNewFile();
        System.out.println(newFile.getAbsolutePath());//E:\aa\a.txt
    }

 复制文件内容:

 public static void main(String[] args) throws Exception{
        File file=new File("F:/aa/a.txt");
        //拷贝文件目录
        File copyFile=deepCopyFile(file);
        //拷贝内容
        startCopyFile(file,copyFile);
    }

    private static void startCopyFile(File file, File copyFile) throws IOException {
        InputStreamReader isr=null;
        OutputStreamWriter osw=null;
        try {
            //先读取目标文件信息,再写入复制文件中
            isr=new InputStreamReader(new FileInputStream(file),"utf-8");
            osw=new OutputStreamWriter(new FileOutputStream(copyFile),"utf-8");
            int len;
            while ((len=isr.read())!=-1){
                osw.write(len);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if(osw!=null){
                osw.close();
            }
            if(isr!=null){
                isr.close();
            }
        }
    }

    private static File deepCopyFile(File file) throws IOException {
        String parent = file.getParent().substring(3);//aa
        File newFile=new File("E:/"+parent);
        //先创建文件夹,不然会报错
        newFile.mkdir();
        newFile=new File(newFile.getAbsolutePath(),file.getName());
        //创建文件
        newFile.createNewFile();
        System.out.println(newFile.getAbsolutePath());//E:\aa\a.txt
        return newFile;
    }

5.文件过滤

案例1:只要后缀为.webp的文件

 public static void main(String[] args) throws Exception{
     Scanner sc=new Scanner(System.in);
        System.out.println("请输入您的路径");
        String s = sc.nextLine();
        File file=new File(s);
        FileFilter fileFilter = new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".webp");
            }
        };
        File[] files = file.listFiles(fileFilter);
        for (File file1 : files) {
            System.out.println(file1.getAbsolutePath());
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值