1.File 类    


1.File 类

1.1.构造方法

  • 文件的 抽象路径名(操作系统无关)
构造方法
格式 说明
File(String filename) 把文件路径名字符串转换为“抽象路径名”,用来 创建 File实例。 filename 为空,报异常“空指针”
File(String parent, String filename) 通过两个路径名字符串拼接后,创建 File 实例。当 filename 为空时,报异常,parent为空效果与上边一致
File(File parent, String filename) 依据抽象路径名parent和filename,创建对象。

  创建指向filename的一个抽象路径名。

 String filename = "a.txt";
String parent = "dir1"; File file = new File(parent);
File file2 = new File(parent, filename);
File file3 = new File(file, filename);

对象创建后,这个filename指向的文件是普通文件、是目录、或者根本不存在,好像没有啥影响。

1.2.常用方法

  • File 类的意义

    • 创建
    • 删除
    • 获取
    • 判断
File 类的方法
 

格式

说明

创建 boolean createNewFile() 把该抽象路径名,创建为普通文件
  boolean mkdir() 把该抽象路径名,创建为目录文件(# mkdir itheima)
  boolean mkdirs() 父目录不存在的情况下,一并创建多个抽象路径名的对象(# mkdir -p xian/JavaEE25/itheima)
删除 boolean delete() 删除该抽象路径名指向的文件。成功删除时,返回 true
获取 long length() 返回该抽象路径名指向的文件的大小。实例1.1
  String getAbsolutePath() 实例1.2,获取该抽象路径名对象的绝对路径名
  File getAbsoluteFile() 实例1.3
  long getFreeSpace() 返回该抽象路径名指向的分区 可用空间
  long getTotalSpace() 返回……分区大小
  String getPath() 返回 抽象路径名 的路径名
  String getName() 返回 抽象路径名 的文件名
  String getParent() 返回 抽象路径名 的父目录名
  File getParentFile() 返回 抽象路径名 的父目录的 “抽象路径名对象”
判断 boolean exists() 返回该抽象路径名在系统中的存在状态。存在时,返回 true
  boolean isFile() 返回该抽象路径名的文件状态。普通文件,返回 true
  boolean isDirectory() 返回该抽象路径名的文件状态。目录文件,返回 true
  boolean isAbsolute() 该抽象路径名是否 指向一个绝对路径的文件。实例1.3
  • 实例 1.1.当抽象路径名指向为空时,返回的 length() 结果是0。
 String filename = "a.txt";
String parent = "dir1"; File file = new File(parent);
File file2 = new File(parent, filename);
File file3 = new File(file, filename); System.out.println("length = " + file3.length()); // 指向为空,0
file.mkdir();
System.out.println(file.length()); //
file2.createNewFile();
System.out.println(file2.length()); //
  • 实例 1.2.获取抽象路径名对象的 “绝对路径名”
 File file = new File("itheima.txt");
file.createNewFile(); // 返回:E:\eclipse_workspace\ItheimaAdvance_Review\itheima.txt
System.out.println(file.getAbsolutePath());
  • 实例 1.3.指向 绝对路径名 的抽象路径名对象
 File fileAbs = file.getAbsoluteFile();
System.out.println(file.isAbsolute()); // false
System.out.println(fileAbs.isAbsolute()); // true
  • 实例 1.4.文件即使不存在,也不影响下边实例的输出结果。
 File file = new File("E:\\itheima.txt");
file.createNewFile(); System.out.println(file.getPath()); // E:\itheima.txt
System.out.println(file.getName()); // itheima.txt
System.out.println(file.getParent()); // E:\
 File file = new File("itheima.txt");
file.createNewFile(); System.out.println(file.getPath()); // itheima.txt
System.out.println(file.getName()); // itheima.txt
System.out.println(file.getParent()); // null
 File file = new File("\\000\\itheima.txt");
// file.createNewFile(); System.out.println(file.getPath()); // \000\itheima.txt
System.out.println(file.getName()); // itheima.txt
System.out.println(file.getParent()); // \000

返回的结果,受到创建 “抽象路径名” 对象时定义的影响。获取 父目录、父目录抽象路径名 时,可能为 null。

1.3.常用方法2

常用方法中(返回数组类型)

格式

说明

String[] list() 该抽象路径名 指向的目录下的 文件名
File[] listFiles() 该抽象路径名 指向的目录,目录下的文件的抽象路径名
  • 分别返回 字符串、抽象路径名对象。
 public class Demo5 {

     public static void main(String[] args) throws IOException {

         String dir = "000";
File directory = new File(dir); // 生成一个目录
if (!directory.exists()) {
directory.mkdir();
}
// 在目录下创建三个普通文件
for (int i = 0; i < 3; i++) {
File file = new File(directory, "itheima" + i + ".txt"); // 局部变量
if (!file.exists()) {
file.createNewFile();
}
} // 抽象路径名file指向的目录下的文件名
String[] file = directory.list();
for (String f : file) {
System.out.println(f);
}
// 直接返回了字符串,只能直接输出了
// itheima0.txt
// itheima1.txt
// itheima2.txt // 抽象路径名file指向的目录,目录中文件的抽象路径名
File[] files = directory.listFiles();
for (File f : files) {
System.out.println(f.getName() + " - " + f.getPath());
}
// 输出结果,因为是个File类,可以做的事情比较多
// itheima0.txt - 000\itheima0.txt
// itheima1.txt - 000\itheima1.txt
// itheima2.txt - 000\itheima2.txt
}
}

1.4.获取目录结构

  • 目的:使用递归,打印指定的文件名构建的 抽象路径名对象 的目录结构
  • 代码:
 public class Demo7 {

     public static void main(String[] args) throws IOException {

         String dirName = "000"; // 存在
// String dirName = "111"; // 不存在
// String dirName = "000\\itheima0.txt"; // 存在
// String dirName = "000\\itheima000.txt"; // 不存在
File file = new File(dirName);
ls_R(file); } private static void ls_R(File file) {
String indentTab = "";
ls_R(file, indentTab); // 打印目录结构时,下一级缩进
} private static void ls_R(File file, String indentTab) {
if (file.isFile()) {
System.out.println(indentTab + file.getName());
// System.out.println(indentTab + file.getPath());
} else if (file.isDirectory()) {
System.out.println(indentTab + file.getPath());
indentTab = indentTab + "\t";
File[] tmp = file.listFiles();
for (File f : tmp) {
ls_R(f, indentTab);
}
} else {
System.out.println("抽象路径名对象不存在!");
}
}
}
  • 输出结果:
 000
000\111
itheima0.txt
itheima1.txt
itheima2.txt
000\222
000\222\333
itheima0.txt
itheima1.txt
itheima2.txt
itheima0.txt
itheima1.txt
itheima2.txt
itheima0.txt
itheima1.txt
itheima2.txt

1.5.模拟下linux下删除目录

  • 模拟Linux下 “rm -frv” 命令的显示效果,代码:
 public class Demo9 {

     public static void main(String[] args) throws IOException {

         String dirName = "000"; // 存在
File file = new File(dirName);
rm_R(file); } private static void rm_R(File file) {
if (file.isFile()) {
// System.out.println("文件正在删除..." + file.getPath());
} else if (file.isDirectory()) {
// System.out.println("目录正在删除..." + file.getPath());
File[] tmp = file.listFiles();
for (File f : tmp) {
rm_R(f);
}
} else {
System.out.println("抽象路径名对象不存在!");
} // 修改成Linux下“rm -frv 000”的现实效果
if (file.isDirectory())
System.out.println("removed directory: '" + file.getPath() + "'");
else if (file.isFile())
System.out.println("removed '" + file.getPath() + "'");
file.delete(); // 删除目录文件、普通文件 }
}

效果:

 [argor@donatello day10 File]$ java Demo9
removed '000/111/itheima2.txt'
removed '000/111/itheima0.txt'
removed '000/111/itheima1.txt'
removed directory: '000/111'
removed '000/itheima2.txt'
removed '000/itheima0.txt'
removed '000/itheima1.txt'
removed '000/222/itheima2.txt'
removed '000/222/itheima0.txt'
removed '000/222/333/itheima2.txt'
removed '000/222/333/itheima0.txt'
removed '000/222/333/itheima1.txt'
removed directory: '000/222/333'
removed '000/222/itheima1.txt'
removed directory: '000/222'
removed directory: ''
[argor@donatello day10 File]$ rm -frv
removed ‘//itheima2.txt’
removed ‘//itheima0.txt’
removed ‘//itheima1.txt’
removed directory: ‘/’
removed ‘/itheima2.txt’
removed ‘/itheima0.txt’
removed ‘/itheima1.txt’
removed ‘//itheima2.txt’
removed ‘//itheima0.txt’
removed ‘///itheima2.txt’
removed ‘///itheima0.txt’
removed ‘///itheima1.txt’
removed directory: ‘//’
removed ‘//itheima1.txt’
removed directory: ‘/’
removed directory: ‘’

A

Java 文件类 File的更多相关文章

  1. java 文件类 null与exists()是不一样的

    java 文件类 null与exists()是不一样的File imageFile0A=null; (imageFile0A==null)与!imageFile0A.exists() 不等价! 一个文 ...

  2. 文件类File

    文件类File继承结构: public class File extends Object implements Serializable, Comparable<File> 常用方法: ...

  3. Java:文件类File的详解

    File类的常用方法: 1.创建     boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false.和输出流不一样,输出流对象一建立创建文件,而 ...

  4. Java输入输出流(IO)-----文件类File详解

       1.java.io.File类简介 凡是与输入.输出相关的类.接口等都定义在java.io包下 File是一个类,可以有构造器创建其对象.此对象对应着一个文件(.txt .avi .doc .p ...

  5. Java I/O(一)流和文件类File的概述、FileInputStream和FileInputStream

    一.流概述 & InputStream.OutputStream 流包括输入流和输出流,即I/O(Input和Output),具体结构如下: I/O类都被放在java.io包中,所有的输入流类 ...

  6. java 文件类操作(转载)

    11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...

  7. 系统学习 Java IO (三)----文件类 File

    目录:系统学习 Java IO---- 目录,概览 Java IO API 中的 File 类可以访问基础文件系统. 使用 File 类,可以: 检查文件或目录是否存在. 如果目录不存在,创建一个目录 ...

  8. java文件操作File类

    1.文件路径操作 测试方法 @Test public void test5() { StringBuffer succBuffer = new StringBuffer("D:\\home\ ...

  9. Java文件类

    在Java语言中,无论是目录还是文件,都抽象成java.io.File类 直接上示例吧 java,io,File的常用操作 删除.创建 因为我的e盘里面是没有这个文件的,所以不存在I哦 创建文件: 获 ...

随机推荐

  1. 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

    问题 rds_content = "{}, 执行了变更,sql语句:{}".format(ExecuteTime, sqls) 'ascii' codec can't encode ...

  2. 开源自然语言处理工具包hanlp中CRF分词实现详解

     CRF简介 CRF是序列标注场景中常用的模型,比HMM能利用更多的特征,比MEMM更能抵抗标记偏置的问题. [gerative-discriminative.png] CRF训练 这类耗时的任务,还 ...

  3. 【java】注释

    一.注释   1. 注释类型 [a]. 单行注释 // 单行注释 [b]. 多行注释 /* 多行注释 */ [c]. 文档注释 /** 文档注释 */ 一般情况下,需求,实现方式用多行注释,类和方法上 ...

  4. ArrayBlcokingQueue,LinkedBlockingQueue与Disruptor三种队列对比与分析

    一.基本介绍 ArrayBlcokingQueue,LinkedBlockingQueue是jdk中内置的阻塞队列,网上对它们的分析已经很多,主要有以下几点: 1.底层实现机制不同,ArrayBlco ...

  5. js写法【3】

    var m=[]; m[m.length]=xx;//相当于push 对Repeat方法提供了8种写法做比较,这一点还是不错的.

  6. 关于 TensorFlow

    TensorFlow 是一个用于人工智能的开源神器 TensorFlow中文社区    http://www.tensorfly.cn/   文档 TensorFlow™ 是一个采用数据流图(data ...

  7. cmd命令记录

    一.查看端口号的使用情况 参考经验:https://jingyan.baidu.com/article/3c48dd34491d47e10be358b8.html 1.netstat -ano,列出所 ...

  8. Nodejs使用多个分隔符分隔字符串

    在nodejs中当需要使用多个分隔符分隔字符串时,可以使用正则表达式作为split函数的参数,具体使用如下: var str = "111@222#333 444@555# 666 777& ...

  9. C语言强化——链表(2)

    目录 链表的应用: 栈 循环队列 C语言实现动态数组 数组实现定长元素个数层次建树 队列实现不定元素个数层次建树 (*) 栈 栈(链表应用) "stack.h" #include ...

  10. c#特性学习(1)

    C#中的特性我认为可以理解为Java中的注解,见名知意,就是描述这个类或是属性的一个信息,是一个语法糖.原理运用的是反射,下面我来演示一下它的原理.其中引用了软谋的代码. 举一个栗子.我们在做code ...