栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java File类的简单使用教程(创建、删除、遍历与判断是否存在等)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java File类的简单使用教程(创建、删除、遍历与判断是否存在等)

前言

Java文件类以抽象的方式代表文件名和目录路径名。该类本身不能用来读数据或写数据,它主要用于磁盘上文件和目录的创建、文件的查找和文件的删除。做一些非读写方面的工作,比如看看文件是否存在、是否可读写及遍历文件目录等等。要想读写数据,必须和其它io流的类配合使用,比如FileInputStream、FileOutputStream等。File对象代表磁盘中实际存在的文件和目录,以下就通过一些简单的列子介绍File的基本使用。

这是整个File简单使用的代码:

1 package com.tianjh;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 
 11 public class FileDemo {
 12 public static void main(String[] args) {
 13 String dirname = "D:/Demo";
 14 // 实例化一个File对象
 15 File f1 = new File(dirname);
 16 
 17 // 1. 遍历指定目录之下的所有文件
 18 // 判断f1对象是否是一个目录
 19 if (f1.isDirectory()) {
 20 System.out.println("Directory of " + dirname);
 21 String[] s = f1.list();
 22 // 遍历s数组,取出数组中的元素进行判断
 23 for (int i = 0; i < s.length; i++) {
 24  File f = new File(dirname + "/" + s[i]);
 25  if (f.isDirectory()) {
 26  System.out.println(s[i] + " is a directory");
 27  } else {
 28  System.out.println(s[i] + " is a file");
 29  }
 30 }
 31 } else {
 32 // 不是一个目录
 33 System.out.println(dirname + " is not a directory");
 34 }
 35 // expected output:
 36 // Directory of D:/Demo
 37 // BufferedInputStream.java is a file
 38 // BufferedOutputStream.java is a file
 39 // childFile is a directory
 40 
 41 
 46 System.out.println(dirname + " allowed to execute? " + f1.canExecute());
 47 // expected output: D:/Demo allowed to execute? true
 48 
 49 
 50 
 56 System.out.println(dirname + " allowed to read? " + f1.canRead());
 57 // expected output: D:/Demo allowed to read? true
 58 
 59 
 65 System.out.println(dirname + " allowed to write? " + f1.canWrite());
 66 // expected output: D:/Demo allowed to write? true
 67 
 68 
 73 String s1 = "C:/Boot";
 74 // “D:/Demo” 与 "C:/Boot" 比较
 75 System.out.println(f1.compareTo(new File(s1)));
 76 // expected output: 1
 77 String s2 = "D:/Deoo";
 78 // “D:/Demo” 与 "D:/Deoo" 比较
 79 System.out.println(f1.compareTo(new File(s2)));
 80 // expected output: -2
 81 
 82 
 83 
 89 File f3 = new File("/Boot");
 90 try {
 91 System.out.println("/Boot file is created? " + f3.createNewFile());
 92 // expected output: /Boot file is created? false
 93 } catch (IOException e) {
 94 e.printStackTrace();
 95 }
 96 
 97 
101 String dirnames = "D:/tmp/boot";
102 File f4 = new File(dirnames);
103 // 创建一个文件夹,成功则返回true,失败则返回false。
104 // 失败表明File对象指定的路径已经存在,或者由于整个路径还不存在,该文件夹不能被创建。
105 System.out.println("create mkdir is " + f4.mkdir());
106 // expected output: create mkdir is true
107 
108 
109 
113 System.out.println("create mkdirs is " + f4.mkdirs());
114 // expected output: create mkdirs is false
115 
116 
117 
122 System.out.println(dirnames + " deleted is " + f4.delete());
123 // expected output: D:/tmp/boot deleted is true
124 
125 
126 
130 System.out.println("getName is " + f1.getName());
131 // expected output: getName is Demo
132 
133 
134 
138 System.out.println("getPath is " + f1.getPath());
139 // expected output: getPath is D:Demo
140 
141 
145 System.out.println("Absolute Path is " + f1.getAbsolutePath());
146 // expected output: Absolute Path is D:Demo
147 
148 
149 
155 System.out.println(f1.exists() ? "exist" : "not");
156 // expected output: exist
157 }
158 
159 }

FileDemo.Java
下面分别介绍常用的几种方法:

1、遍历指定目录之下的所有文件( 遍历" D:/Demo "中的所有文件及目录)

D磁盘中Demo目录的结果如下所示:

示例代码:

String dirname = "D:/Demo";
 // 实例化一个File对象
 File f1 = new File(dirname);

 // 1. 遍历指定目录之下的所有文件
 // 判断f1对象是否是一个目录
 if (f1.isDirectory()) {
  System.out.println("Directory of " + dirname);
  String[] s = f1.list();
  // 遍历s数组,取出数组中的元素进行判断
  for (int i = 0; i < s.length; i++) {
  File f = new File(dirname + "/" + s[i]);
  if (f.isDirectory()) {
   System.out.println(s[i] + " is a directory");
  } else {
   System.out.println(s[i] + " is a file");
  }
  }
 } else {
  // 不是一个目录
  System.out.println(dirname + " is not a directory");
 }
 // expected output:
 // Directory of D:/Demo
 // BufferedInputStream.java is a file
 // BufferedOutputStream.java is a file
 // childFile is a directory

输出结果:

2、测试指定文件是否可执行


 System.out.println(dirname + " allowed to execute? " + f1.canExecute());
 // expected output: D:/Demo allowed to execute? true

3、测试指定文件是否可读取


 System.out.println(dirname + " allowed to read? " + f1.canRead());
 // expected output: D:/Demo allowed to read? true

4、测试指定文件是否可写


 System.out.println(dirname + " allowed to write? " + f1.canWrite());
 // expected output: D:/Demo allowed to write? true

样例2、3、4的结果可参考Demo 的属性

5、比较抽象路径名和参数抽象路径名是否相等,根据字典顺序进行比较


 String s1 = "C:/Boot";
 // “D:/Demo” 与 "C:/Boot" 比较
 System.out.println(f1.compareTo(new File(s1)));
 // expected output: 1
 String s2 = "D:/Deoo";
 // “D:/Demo” 与 "D:/Deoo" 比较
 System.out.println(f1.compareTo(new File(s2)));
 // expected output: -2

结果:

6、创建一个新文件


 File f3 = new File("/Boot");
 try {
  System.out.println("/Boot file is created? " + f3.createNewFile());
  // expected output: /Boot file is created? false
 } catch (IOException e) {
  e.printStackTrace();
 }

结果:

7、创建一个目录


 String dirnames = "D:/tmp/boot";
 File f4 = new File(dirnames);
 // 创建一个文件夹,成功则返回true,失败则返回false。
 // 失败表明File对象指定的路径已经存在,或者由于整个路径还不存在,该文件夹不能被创建。
 System.out.println("create mkdir is " + f4.mkdir());
 // expected output: create mkdir is true

结果:

8、创建一个目录,包括其不存在的父级目录,因为在上列中创建了对应的目录文件,所有mkdirs创建就返还false


System.out.println("create mkdirs is " + f4.mkdirs());
// expected output: create mkdirs is false

9、删除文件或者目录(删除前面创建的/tmp路径下的boot)


 System.out.println(dirnames + " deleted is " + f4.delete());
 // expected output: D:/tmp/boot deleted is true

结果:

10、取得抽象路径的名称


 System.out.println("getName is " + f1.getName());
 // expected output: getName is Demo

结果:

11、取得抽象路径的字符串


 System.out.println("getPath is " + f1.getPath());
 // expected output: getPath is D:Demo

结果:

12、取得抽象路径的绝对路径


 System.out.println("Absolute Path is " + f1.getAbsolutePath());
 // expected output: Absolute Path is D:Demo

结果:

13、判断抽象路径指定的文件或目录是否存在


 System.out.println(f1.exists() ? "exist" : "not");
 // expected output: exist

结果:

到此这篇关于Java File类简单使用(创建、删除、遍历与判断是否存在等)的文章就介绍到这了,更多相关Java File类简单使用内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129841.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号