当前位置:   article > 正文

java.io.File类介绍

java.io.File类介绍

java.io.File 类是 Java IO 包中用于表示文件或目录路径的类。它提供了许多方法,用于操作文件和目录的属性、路径、名称等信息,但它并不提供用于文件内容读写的方法,这些读写操作通常通过其他类来完成。

以下是 java.io.File 类的一些常用方法:

  1. 创建文件或目录

    • boolean createNewFile():创建一个新的空文件。
    • boolean mkdir():创建一个新的目录。
  2. 删除文件或目录

    • boolean delete():删除文件或目录。
  3. 获取文件或目录信息

    • String getName():获取文件或目录的名称。
    • String getPath():获取文件或目录的路径。
    • String getAbsolutePath():获取文件或目录的绝对路径。
    • long length():获取文件的长度(字节数)。
    • boolean isFile():判断是否为文件。
    • boolean isDirectory():判断是否为目录。
    • boolean exists():判断文件或目录是否存在。
  4. 遍历目录

    • String[] list():返回目录中所有文件和子目录的名称数组。
    • File[] listFiles():返回目录中所有文件和子目录的 File 对象数组。
  5. 其他方法

    • boolean renameTo(File dest):重命名文件或目录。
    • boolean canRead():判断文件是否可读。
    • boolean canWrite():判断文件是否可写。
    • boolean setReadOnly():将文件设置为只读。

java.io.File 类提供了一种便捷的方式来操作文件和目录的属性和信息,它与平台无关,可以在不同的操作系统上使用。但需要注意的是,它只是一个路径名的抽象表示,并不代表文件本身的内容。因此,要读取或写入文件内容,需要使用其他 IO 类(如 FileInputStreamFileOutputStreamBufferedReaderBufferedWriter 等)。

  1. import java.io.File;
  2. import java.io.IOException;
  3. public class Main {
  4. public static void main(String[] args) {
  5. // 创建 File 对象表示文件或目录
  6. File file = new File("example.txt");
  7. File directory = new File("exampleDir");
  8. try {
  9. // 创建文件
  10. if (file.createNewFile()) {
  11. System.out.println("File created: " + file.getAbsolutePath());
  12. } else {
  13. System.out.println("File already exists.");
  14. }
  15. // 创建目录
  16. if (directory.mkdir()) {
  17. System.out.println("Directory created: " + directory.getAbsolutePath());
  18. } else {
  19. System.out.println("Directory already exists.");
  20. }
  21. // 输出文件和目录信息
  22. System.out.println("File name: " + file.getName());
  23. System.out.println("File path: " + file.getPath());
  24. System.out.println("File absolute path: " + file.getAbsolutePath());
  25. System.out.println("File length: " + file.length() + " bytes");
  26. System.out.println("Is file: " + file.isFile());
  27. System.out.println("Is directory: " + file.isDirectory());
  28. System.out.println("Directory name: " + directory.getName());
  29. System.out.println("Directory path: " + directory.getPath());
  30. System.out.println("Directory absolute path: " + directory.getAbsolutePath());
  31. System.out.println("Is file: " + directory.isFile());
  32. System.out.println("Is directory: " + directory.isDirectory());
  33. // 删除文件和目录
  34. if (file.delete()) {
  35. System.out.println("File deleted: " + file.getAbsolutePath());
  36. } else {
  37. System.out.println("Failed to delete file.");
  38. }
  39. if (directory.delete()) {
  40. System.out.println("Directory deleted: " + directory.getAbsolutePath());
  41. } else {
  42. System.out.println("Failed to delete directory.");
  43. }
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/598979
推荐阅读
相关标签
  

闽ICP备14008679号