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. win10:如何开启自带虚拟机

    1.首先要找到控制面板,我们点开windows键,然后选择在所有应用中找到“Windows 系统”,打开之后,我们找到“控制面板”,打开.     2.打开控制面板之后,我们选择程序,如图示.   3 ...

  2. max_result_window

    PUT http://192.168.1.12:9200/_settings { "index": { "max_result_window": "1 ...

  3. WyBox使用shell脚本检测io口是否正常

    1. 修改mt7620a.dts文件,把gpio管脚复用全都定义为通用gpio /dts-v1/; /include/ "mt7620a.dtsi" / { compatible ...

  4. ManualResetEvent学习实例

    ManualResetEvent为多个线程之间提供了一个共享的信号. 初始化:ManualResetEvent mre=new ManualResetEvent(true) 初始值为true表示有信号 ...

  5. pytest.5.参数化的Fixture

    From: http://www.testclass.net/pytest/parametrize_fixture/ 背景 继续上一节的测试需求,在上一节里,任何1条测试数据导致断言不通过后测试用例就 ...

  6. MyBatis 值的传递

    1.值的传递 - Map传值 可以通过对象获取Map传递值,在配置文件中通过 #{} 或 ${} 进行应用 查询30-40岁的用户 <!-- 值的传递 - Map传值 --> <se ...

  7. Jmeter(十一)参数化

    有关参数化的概念,前面有篇随笔已经粗略的提到了参数化的一点内容.本篇来主要记录参数化. Jmeter的参数化方式有很多,在此我来一一记录,对待不同个规模.业务模型.数据量来进行记录参数化的方法. 一. ...

  8. vue 路由 以及默认路由跳转

    https://router.vuejs.org/ vue路由配置: 1.安装 npm install vue-router --save / cnpm install vue-router --sa ...

  9. Abp.Linq.Extensions扩展(1)--static class QueryableExtensions

    // 摘要:        //     Used for paging with an Abp.Application.Services.Dto.IPagedResultRequest object ...

  10. CRM 2016 升级CRM365之注意事项

    https://docs.microsoft.com/zh-cn/previous-versions/dynamicscrm-2016/deployment-administrators-guide/ ...