Java 文件类 File
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 类的意义
- 创建
- 删除
- 获取
- 判断
格式 |
说明 |
|
| 创建 | 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的更多相关文章
- java 文件类 null与exists()是不一样的
java 文件类 null与exists()是不一样的File imageFile0A=null; (imageFile0A==null)与!imageFile0A.exists() 不等价! 一个文 ...
- 文件类File
文件类File继承结构: public class File extends Object implements Serializable, Comparable<File> 常用方法: ...
- Java:文件类File的详解
File类的常用方法: 1.创建 boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false.和输出流不一样,输出流对象一建立创建文件,而 ...
- Java输入输出流(IO)-----文件类File详解
1.java.io.File类简介 凡是与输入.输出相关的类.接口等都定义在java.io包下 File是一个类,可以有构造器创建其对象.此对象对应着一个文件(.txt .avi .doc .p ...
- Java I/O(一)流和文件类File的概述、FileInputStream和FileInputStream
一.流概述 & InputStream.OutputStream 流包括输入流和输出流,即I/O(Input和Output),具体结构如下: I/O类都被放在java.io包中,所有的输入流类 ...
- java 文件类操作(转载)
11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...
- 系统学习 Java IO (三)----文件类 File
目录:系统学习 Java IO---- 目录,概览 Java IO API 中的 File 类可以访问基础文件系统. 使用 File 类,可以: 检查文件或目录是否存在. 如果目录不存在,创建一个目录 ...
- java文件操作File类
1.文件路径操作 测试方法 @Test public void test5() { StringBuffer succBuffer = new StringBuffer("D:\\home\ ...
- Java文件类
在Java语言中,无论是目录还是文件,都抽象成java.io.File类 直接上示例吧 java,io,File的常用操作 删除.创建 因为我的e盘里面是没有这个文件的,所以不存在I哦 创建文件: 获 ...
随机推荐
- 深入理解ASP.NET MVC(7)
系列目录 Action的定位 再次回到Controller的ExecuteCore方法,回到action调用的入口: 1 if (!ActionInvoker.InvokeAction(Control ...
- mysql主从复制--重置操作reset master, reset slave
本文介绍reset master, reset slave的作用. reset master 在master上执行 mysql > RESET MASTER 作用包括: 删除binlog索引文件 ...
- RedHat6.5安装kafka单机
版本号: Redhat6.5 JDK1.8 zookeeper-3.4.6 kafka_2.11-0.8.2.1 1.软件环境 已经搭建好的zookeeper: RedHat6.5 ...
- jquery.form插件中动态修改表单数据
jquery.form jquery.form插件(http://malsup.com/jquery/form/)是大家经常会用到的一个jQuery插件,它可以很方便将表单转换为ajax的方式进行提交 ...
- DEVC++ C++ Builder6.0
Devc++安装后无法正常编译程序 出现错误,不知道是什么,可能是不兼容的原因 然后就是一直编译出错,程序是最简单的helloworld程序. 之后选择安装C++ Builder 6.0
- InfluxDB 1.6文档
警告!此页面记录了不再积极开发的InfluxDB的早期版本.InfluxDB v1.7是InfluxDB的最新稳定版本. InfluxDB是一个时间序列数据库,旨在处理高写入和查询负载.它是TICK堆 ...
- TextBox限制输入字母、数字、退格键
公共方法如下: /// <summary> /// 正则表达式验证只能输入数字或字母 /// </summary> /// <param name="pendi ...
- PHP_EOL DIRECTORY_SEPARATOR
换行符 PHP_EOL unix系列用 \n windows系列用 \r\n mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 路径上的斜杠 DIRECTORY_ ...
- 开启Unity3D之旅
圣典知识目录http://game.ceeger.com/Manual/ NGUI http://www.taikr.com/course/34泰课课程 http://tieba.baidu.com/ ...
- 使用RetionalRose根据现有的java工程逆向生成类图
1.进入RetionalRose选择J2EE模板 2.在菜单栏选择tools->java/j2EE->reverse engineer 3.编辑路径Edit CLASSPATH选择要生成类 ...