第六章:File类的使用

第六章:File类的使用

File的概述

  • File类和四大家族没有关系,所以File类不能完成文件的读写。

  • File对象代表什么??
    一个File对象可能对应的是目录,也可能是文件本身的文件名。
    File只是一个路径名的抽象表示形式

  • 需要掌握File类中常用的方法

    File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
    boolean exists() 测试此抽象路径名表示的文件或目录是否存在。
    boolean createNewFile() 当且仅当不存在具有此抽象路径名指定名称的文件时,创建一个新的空文件。
    boolean mkdir() 创建此抽象路径名指定的目录。
    boolean mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
    String getParent() 返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
    File getParentFile() 返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。
    String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串。
    
    public class FileTest01 {
          
          
        public static void main(String[] args) throws Exception {
          
          
            //创建一个File对象
            File f1 = new File("E:\\file");
            //判断是否存在
            System.out.println(f1.exists());
    
            //如果E:\file不存在,则以文件的方式创建出来
    //        if (!f1.exists()){
          
          
    //            //以文件的方式创建
    //            f1.createNewFile();
    //        }
            //如果E:\file不存在,则以目录的方式创建出来
    //        if (!f1.exists()){
          
          
    //            //以目录的形式创建
    //            f1.mkdir();
    //        }
            //可以创建多重目录吗
            File f2 = new File("E:/a/b/c/d/e");
    //        if (!f2.exists()){
          
          
    //            //创建多重目录
    //            f2.mkdirs();
    //        }
            File f3 = new File("E:\\Workspace\\Study\\Copy02.java");
            //获取该文件的父路径名
            System.out.println(f3.getParent());
            File parentFile = f3.getParentFile();
            System.out.println("获取绝对路径:" + parentFile.getAbsolutePath());
    
            File f4 = new File("copy");
            //获取一个文件的绝对路径
            System.out.println(f4.getAbsolutePath());
        }
    }
    
    /*
        File类的常用方法
     */
    public class FileTest02 {
          
          
        public static void main(String[] args) {
          
          
            File f1 = new File("copy");
            //获取文件名
            System.out.println(f1.getName());
    
            //判断是否是一个目录
            System.out.println(f1.isDirectory());//false
    
            //判断是否是一个文件
            System.out.println(f1.isFile());//true
    
            //获取文件最后一次修改时间
            System.out.println(f1.lastModified());//这是毫秒,是从1970年到现在的总毫秒数
            //将总毫秒数转换成日期
            Date time = new Date(f1.lastModified());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSS");
            System.out.println(sdf.format(time));
    
            //获取文件大小(获取的是字节数)
            System.out.println(f1.length());
        }
    }
    
    /*
        File中的listFile方法
     */
    public class FileTest03 {
          
          
        public static void main(String[] args) {
          
          
            //File[] listFiles()
            //获取当前目录下所有的子文件
            File f = new File("E:\\Workspace\\Study");
            File[] files  = f.listFiles();
            for (File file:files) {
          
          
                //System.out.println(file.getAbsolutePath());
                System.out.println(file.getName());
            }
        }
    }
    

如何集合IO流完成整个目录的拷贝

  • IO流中只能完成一个文件的拷贝。

  • 通过File类完成目录的拷贝(即将源文件的目录结构创建出来)

  • 同构IO流将目录中的文件拷贝出来。

    /*
        目录拷贝
     */
    public class HomeTest02 {
          
          
        public static void main(String[] args) {
          
          
            //源文件
            File sirFile = new File("E:\\sougouinput\\SogouInput");
            //拷贝目标
            File descFile = new File("D:\\");
            //调用方法拷贝
            copyDir(sirFile,descFile);
        }
        private static void copyDir(File sirFile, File descFile) {
          
          
            //给出递归的结束条件,即当所处绝对路径不是目录而是文件时
            if (sirFile.isFile()){
          
          
                //当是一个文件的时候,此时执行文件的复制工作
                FileInputStream in = null;//文件读取工作
                FileOutputStream out = null;//文件写入工作	
                try {
          
          
                    in = new FileInputStream(sirFile);
                    //拼接后的绝对路径
                    String descDir = (descFile.getAbsolutePath().endsWith("\\") ?
                            descFile.getAbsolutePath():descFile.getAbsolutePath() +"\\") +
                            sirFile.getAbsolutePath().substring(3);
                    out = new FileOutputStream(descDir);
                    byte[] bytes = new byte[1024 * 1024];
                    int readCount = 0;
                    while ((readCount = in.read(bytes)) != -1){
          
          
                        out.write(bytes,0,readCount);
                    }
                    out.flush();
                } catch (FileNotFoundException e) {
          
          
                    e.printStackTrace();
                } catch (IOException e) {
          
          
                    e.printStackTrace();
                }finally {
          
          
                    if (in != null) {
          
          
                        try {
          
          
                            in.close();
                        } catch (IOException e) {
          
          
                            e.printStackTrace();
                        }
                    }
                    if (out != null) {
          
          
                        try {
          
          
                            out.close();
                        } catch (IOException e) {
          
          
                            e.printStackTrace();
                        }
                    }
                }
                return;
            }
            //先查看文件目录
            File[] files = sirFile.listFiles();
            for (File file:files) {
          
          
                //当发现这个文件时目录的时候,我们应该创建新目录
                if (file.isDirectory()){
          
          
                    //先获取这个文件的绝对路径,然后进行字符创切分和拼接
                    String sirDir = file.getAbsolutePath().substring(3);
                    //拼接后的绝对路径
                    String descDir = (descFile.getAbsolutePath().endsWith("\\") ?
                            descFile.getAbsolutePath():descFile.getAbsolutePath() +"\\") + sirDir;
                    System.out.println(descDir);
                    //利用绝对路径先把目录创建好(当这个路径不存在时创建这个路径)
                    File newFile = new File(descDir);
                    if (!newFile.exists()){
          
          
                        newFile.mkdirs();
                    }
                }
                //表层目录创建完成后,需要递归的创建目录中的子目录
                copyDir(file,descFile);
            }
        }
    }
    

猜你喜欢

转载自blog.csdn.net/qq_28384023/article/details/109722196