# File文件操作
[TOC]
## File类介绍
`java.io.File`类是一个与文件本身操作有关的类,此类可以实现文件创建,删除,重命名,取得文件大小,修改日期等常见的系统文件操作
File类的常用方法
| 方法 | 描述 |
| --- | --- |
| public File(String pathName) | 给定一个要操作文件的完整路径 |
| public File(File parent,String child) | 给定要操作文件的父路径和子文件名称 |
| public boolean creatNewFile() throw IOException | 创建文件 |
| public boolean delete() | 删除文件 |
| public boolean exists() | 判断给定路径是否存在 |
| public boolean mkdir() | 建此抽象路径名指定的目录。 |
| public boolean mkdirs() | 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。 |
## File类的使用
### 创建一个新文件
~~~
/**
* 创建一个新文件
* @param path
* @throws IOException
*/
public static void createNewFile(String path) throws IOException {
File file = new File(path);
boolean createFlag = file.createNewFile();
System.out.println(createFlag);
}
~~~
### 删除一个文件
~~~
/**
* 删除一个文件
* @param path
* @throws IOException
*/
public static void deleteFile(String path) throws IOException {
File file = new File(path);
boolean delteFlag = file.delete();
// 返回true表示删除成功 false表示删除失败,失败的原因大部分是文件不存在
System.out.println(delteFlag);
}
~~~
### 创建目录
~~~
/**
* 创建目录
* @param path
*/
public static void mkdir(String path) {
File file = new File(path);
System.out.println(file.mkdir());
}
~~~
### 创建目录(如果父目录不存在则创建父目录)
~~~
/**
* 创建目录(如果父目录不存在,则创建父目录)
* @param path
*/
public static void mkdirs(String path) {
File file = new File(path);
System.out.println(file.mkdirs());
}
~~~
### 判断一个文件或者路径是否存在
~~~
/**
* 判断一个文件后者路径是否存在
* @param path
*/
public static void existsFile(String path) {
File file = new File(path);
System.out.println(file.exists());
}
~~~
### 获取文件的绝对路径
~~~
/**
* 获取文件的绝对路径
* @param path
*/
public static void getAbsolutePath(String path) {
File file = new File(path);
System.out.println(file.getAbsolutePath());
}
~~~
### 获取文件名
~~~
/**
* 获取文件名
* @param path
*/
public static void getFileName(String path) {
File file = new File(path);
System.out.println(file.getName());
}
~~~
### 获取父路径
~~~
/**
* 获取父路径
*/
public static void getParent(String path) {
File file = new File(path);
System.out.println(file.getParent());
}
~~~
### 判断一个文件对象是否是目录
~~~
/**
* 判断一个文件对象是否是目录
*/
public static void isDirectory(String path) {
File file = new File(path);
System.out.println(file.isDirectory());
}
~~~
### 判断一个文件对象是否是文件
~~~
/**
* 判断一个文件对象是否是文件
* @param path
*/
public static void isFile(String path) {
File file = new File(path);
System.out.println(file.isFile());
}
~~~
### 获取文件目录下的所有文件和目录清单
~~~
/**
* 获取文件目录下的所有文件和目录清单
* @param path
*/
public static void list(String path) {
File file = new File(path);
boolean flag = file.isDirectory();
if(!flag) {
String[] files = file.list();
for (String fileName : files) {
System.out.println(fileName);
}
} else {
System.out.println("not directory");
}
}
~~~
### 获取文件目录下的所有文件和目录清单(文件名)
~~~
/**
* 获取文件目录下的所有文件和目录清单(文件名)
* @param path
*/
public static void listFiles(String path) {
File file = new File(path);
File[] files = file.listFiles();
for (File fileName : files) {
System.out.println(fileName.getAbsolutePath());
}
}
~~~
### 文件重命名
~~~
/**
* 文件重命名
* @param oldFileName
* @param newFileName
*/
public static void renameTo(String oldFileName, String newFileName) {
File oldFile = new File(oldFileName);
File newFile = new File(newFileName);
System.out.println(oldFile.renameTo(newFile));
}
~~~
### 设置文件只读
~~~
/**
* 设置文件只读
* @param path
*/
public static void setReadonly(String path) {
File file = new File(path);
System.out.println(file.setReadOnly());
}
~~~
### 文件的递归读取
~~~
public static void main(String[] args) {
printAllFile(new File("d:" + File.separator + "fileTest"));
}
public static void printAllFile(File file) {
String filePath = file.getAbsolutePath();
System.out.println(filePath);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File lf : files) {
printAllFile(lf);
}
}
}
~~~
### 文件的递归删除
~~~
public static void main(String[] args) {
deleteAllFile(new File("d:" + File.separator + "fileTest"));
}
public static void deleteAllFile(File file) {
if (file.isFile()) {
// 直接删除文件
file.delete();
} else {
File[] files = file.listFiles();
for (File fl : files) {
deleteAllFile(fl);
}
// 删除目录
file.delete();
}
}
~~~