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. 微信小程序学习之for循环

    一.使用自定义创建的json数据 1. 创建微信小程序项目后 在wxml文件中增加for循环 <block wx:for="{{posts_key}}" wx:for-ite ...

  2. VMware Ubuntu如何连接互联网

    Brigde——桥接 :默认使用VMnet0  1.原理:  Bridge 桥”就是一个主机,这个机器拥有两块网卡,分别处于两个局域网中,同时在”桥”上,运行着程序,让局域网A中的所有数据包原封不动的 ...

  3. Spring Cloud(Dalston.SR5)--Hystrix 断路器

    Spring Cloud 对 Hystrix 进行了封装,使用 Hystrix 是通过 @HystrixCommand 注解来使用的,被 @HystrixCommand 注解标注的方法,会使用 Asp ...

  4. Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用搭建

    高可用集群 在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对与微服务和服务注册中心都需要高可用部署,Eureka 高可用实际上就是将自己 ...

  5. [转]python实现RESTful服务(基于flask)

    python实现RESTful服务(基于flask) 原文: https://www.jianshu.com/p/6ac1cab17929  前言 上一篇文章讲到如何用java实现RESTful服务, ...

  6. pytest.6.Parametrize Fixture

    From: http://www.testclass.net/pytest/parametrizing_fixture/ 背景 @pytest.mark.parametrize 装饰器可以让我们每次参 ...

  7. ALGO-151_蓝桥杯_算法训练_6-2递归求二进制表示位数

    记: 进制转换 AC代码: #include <stdio.h> #define K 2 int main(void) { ; scanf("%d",&n); ...

  8. what's the difference between grouping and facet in lucene 3.5

    I  found in lucene 3.5 contrib folder two plugins: one is grouping, the other is facet. In my option ...

  9. 【Linux】使用fsck对磁盘进行修复

    在后台执行 磁盘修复 nohup fsck.ext3 -y /dev/sdb1 > /root/fsck.log 2>&1 & 使用nohup和& 让进程在后台执行 ...

  10. Easyloggingpp的使用

    对于有开发经验的程序员来说,记录程序执行日志是一件必不可少的事情.通过查看和分析日志信息,不仅可以有效地帮助我们调试程序,而且当程序正式发布运行之后,更是可以帮助我们快速.准确地定位问题.在现在这个开 ...