一、简介

  1. File类和四大家族没有关系,所有File类不能完成文件的读和写
  2. File对象是什么

image.png

二、常用方法

1.

  1. package javase.day31.io;
  2. import java.io.File;
  3. import java.io.IOException;
  4. public class FileTest01 {
  5. public static void main(String[] args) throws IOException {
  6. //创建一个File对象
  7. File f1 = new File("D\\file");
  8. //判断是否存在
  9. System.out.println(f1.exists());
  10. //如果不存在,则以文件的形式创建出来
  11. // if (!f1.exists()){
  12. // f1.createNewFile();
  13. // }
  14. //以目录的形式新建
  15. // if (!f1.exists()){
  16. // f1.mkdir();
  17. // }
  18. // //创建多重目录
  19. // File f2 = new File("D:/a/b/c");
  20. // if (!f2.exists()){
  21. // f2.mkdirs();
  22. // }
  23. //获取文件的父路径
  24. File f3 = new File("");
  25. }
  26. }

2

image.png

3

image.png

4.获取文件最后一次修改时间

image.png

5.获取文件大小

image.png

6.listFile方法(获取当前目录下所有的子文件)

image.png

7.负责文件

  1. package day31.homework;
  2. import java.io.*;
  3. public class CopyAll {
  4. public static void main(String[] args) {
  5. //拷贝源
  6. File srcFile = new File("D:\\学习ppt");
  7. //拷贝目标
  8. File destFile = new File("C:\\");
  9. //拷贝方法
  10. copyDir(srcFile,destFile);
  11. }
  12. /**
  13. *
  14. * @param srcFile 拷贝源
  15. * @param destFile 拷贝目标
  16. */
  17. private static void copyDir(File srcFile, File destFile) {
  18. if (srcFile.isFile()){
  19. FileInputStream in = null;
  20. FileOutputStream out = null;
  21. try {
  22. //读这个文件
  23. in = new FileInputStream(srcFile);
  24. //写到这个文件中
  25. String path = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath(): destFile.getAbsolutePath()+"\\") + srcFile.getAbsolutePath().substring(3);
  26. out = new FileOutputStream(path);
  27. //一边读一边写
  28. byte[] bytes = new byte[1024 * 1024];
  29. int readCount = 0;
  30. while ((readCount = in.read(bytes))!= -1){
  31. out.write(bytes,0,readCount);
  32. }
  33. out.flush();
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. } finally {
  39. if (out != null){
  40. try {
  41. out.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. if (in != null){
  47. try {
  48. in.close();
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54. //文件就不用调用,递归结束
  55. return;
  56. }
  57. //获取源下面的子目录
  58. File[] files = srcFile.listFiles();
  59. for (File file : files){
  60. // 获取所有文件的绝对路径
  61. // System.out.println(file.getAbsolutePath());
  62. if (file.isDirectory()){
  63. //新建目录
  64. String srcDir = file.getAbsolutePath();
  65. // String destDir = "C:\\"+ srcDir.substring(3);
  66. //动态获取
  67. String destDir = (destFile.getAbsolutePath().endsWith("\\")? destFile.getAbsolutePath(): destFile.getAbsolutePath()+"\\") + srcFile.getAbsolutePath().substring(3);
  68. System.out.println(destDir);
  69. File newFile = new File(destDir);
  70. if (!newFile.exists()){
  71. newFile.mkdirs();
  72. }
  73. }
  74. //递归
  75. copyDir(file,destFile);
  76. }
  77. }
  78. }