Java基础(十六)——文件

28 篇文章 5 订阅

文件和文件夹管理
File类
在这里插入图片描述
只是创建一个对象,不会创建文件。
创建一个文件使用f.creatNewFile();
且只能创建文件,不能创建文件夹。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

练习:输出指定文件下的所有Java文件(包含子目录),删除文件夹

import java.io.File;

public class Demo {
    public static void main(String[] args) {
        File f = new File("C:\\Users\\50339\\Documents\\TIME1");
        deleteFile(f);
    }

    //输出文件下的所有文件(包含子文件)
    public static void method(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for(File f : files){
                if(f.isFile()){
                    if(f.getName().endsWith(".java")){
                        System.out.println(f.getName());
                    }
                }
                //子文件夹下的文件
                else{
                    method(f);
                }
            }

        }
    }

    //删除指定目录下的所有文件和文件夹
    public static void deleteFile(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for(File f: files){
                if(f.isFile()){
                    f.delete();
                }
                else if(f.isDirectory())
                    System.out.println(f.getName());
                    deleteFile(f);
            }
            System.out.println(file.getName());
            file.delete();
        }
    }
}

在这里插入图片描述
练习:复制文件
没有文件可以创建文件,但是不会自动创建文件夹,如果没有就会报错。

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyFile {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("C:\\Users\\50339\\IdeaProjects\\Praction\\src\\Test.java");
        FileWriter fw = new FileWriter("C:\\Copy\\Test.java");
        //mechod(fr, fw);
        method2(fr, fw);

        fw.close();
        fr.close();

    }

    public static void method2(FileReader fr, FileWriter fw) throws IOException {
        //一次读写一个字符数组
        int len;
        char[] chs = new char[1024];
        while((len = fr.read(chs)) != -1){
            fw.write(chs,0,len);
            fw.flush();
        }
    }

    public static void mechod(FileReader fr, FileWriter fw) throws IOException {
        //一次读写一个字符
        int ch;
        while((ch = fr.read())!= -1){
            fw.write(ch);
            fw.flush();
        }
    }
}

练习:复制图片(只能使用字节流)
在这里插入图片描述
字节流不需要刷新,直接写入文件

import java.io.*;

public class CopyPicture {
    public static void main(String[] args) throws IOException {
        method();
        method2();

        return;
    }

    //字节流可以复制图片
    public static void method2() throws IOException {
        FileInputStream fis = new FileInputStream("C:\\Users\\50339\\IdeaProjects\\Praction\\src\\test.png");
        FileOutputStream fos = new FileOutputStream("C:\\CopyPicture\\zijie.png");
        int len;
        byte[] b = new byte[1024];
        while((len = fis.read(b)) != -1)
        {
            fos.write(b,0,len);
        }
        fos.close();
        fis.close();
    }

    //字节流不能复制图片
    public static void method() throws IOException {
        FileReader fr = new FileReader("C:\\Users\\50339\\IdeaProjects\\Praction\\src\\test.png");
        FileWriter fw = new FileWriter("C:\\CopyPicture\\jietu.png");

        int len;
        char[] chs = new char[1024];
        while((len = fr.read(chs)) != -1){
            fw.write(chs,0,len);
            fw.flush();
        }

        fw.close();
        fr.close();
    }
}

在这里插入图片描述
String构造源码:没有文件,创建一个文件new File

public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }

File构造:没有文件抛出异常

public FileInputStream(File file) throws FileNotFoundException {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        fd = new FileDescriptor();
        fd.attach(this);
        path = name;
        open(name);
        FileCleanable.register(fd);       // open set the fd, register the cleanup
    }

字节流复制文本文件
在这里插入图片描述

字符串中的文件路径有两种方式:

“C:/a.txt"
"C:\\a.txt"

在这里插入图片描述
第二种创建文件夹比较安全。

剩下的自己看文档。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值