一、File类
1.File的一个对象,可以代表一个文件或文件夹(文件目录)。
2.File在java.io下
二、创建File的实例
- new File(String pathname)
pathname为绝对路径/相对路径
以pathname为路径创建File对象 - new File(File parent, String child)
以parent为父路径,child为子路径创建File对象 - new File(String parent, String child)
根据一个父File对象和子文件路径创建File对象
三、常用的方法
1.File类的获取功能
- String getAbsolutePath() 获取绝对路径 *
- String getPath() 获取路径
- String getName() 获取名称
- String getParent() 获取上层文件目录路径,没有返回null *
- long length() 获取文件的字节数
- long lastModified() 获取最后一次修改时间(毫秒)
- String[] list() 获取指定目录先文件或目录的名称数组
- File[] listFiles() 获取指定目录先文件或目录的File(路径)数组
代码举例:
import java.io.File;
import java.util.Arrays;
import javax.swing.text.AsyncBoxView.ChildState;
public class FileClass_use {
public static void main(String[] args) {
FileClass_use fu = new FileClass_use();
fu.test();
}
public void test() {
//new File(String pathname)
File file = new File("E:\\Java_IOStreamPath\\test1");
//以parent为父路径,child为子路径创建File对象
File file2 = new File("E:\\Java_IOStreamPath", "Fold");
//new File(String parent, String child)
File file3 = new File(file2, "tets2");
System.out.println(file);
System.out.println(file2);
System.out.println(file3);
System.out.println("------------------------------------");
//String getAbsolutePath() 获取绝对路径
System.out.println("file的绝对路径:"+file.getAbsolutePath()+"\n");
//String getPath() 获取路径
System.out.println("file的path:"+file.getPath()+"\n");
//String getName() 获取名称
System.out.println("file的name:"+file.getName()+"\n");
//String getParent() 获取上层文件目录路径,没有返回null *
System.out.println("file的上级目录:"+file.getParent()+"\n");
//long length() 获取文件的字节数
System.out.println("file的字节数:"+file.length()+"\n");
//String[] list() 获取指定目录先文件或目录的名称数组
String arr[] = file2.list();
for (String string : arr) {
System.out.println(string);
}
//File[] listFiles() 获取指定目录先文件或目录的File(路径)数组
System.out.println();
File files[] = file2.listFiles();
for (File file4 : files) {
System.out.println(file4);
}
}
}
2.File类的重命名
- boolean file.renameTo(File2 dest)
同级目录下表示重命名,不同级目录表示剪切
要想成功,file1必须存在,file2不能存在
代码举例:
import java.io.File;
import java.util.Arrays;
import javax.swing.text.AsyncBoxView.ChildState;
public class FileClass_use {
public static void main(String[] args) {
FileClass_use fu = new FileClass_use();
fu.test();
}
public void test() {
//new File(String pathname)
File file = new File("E:\\Java_IOStreamPath\\test1.txt");
//以parent为父路径,child为子路径创建File对象
File file2 = new File("E:\\Java_IOStreamPath", "Fold");
File file3 = new File("E:\\Java_IOStreamPath\\Fold\\test1_rename.txt");
System.out.println("执行file.renameTo(file3)前,E:\\Java_IOStreamPath\\Fold目录下的内容:");
String[] list = file2.list();
for (String string : list) {
System.out.println(string);
}
//boolean renameTo(File dest) 把文件重命名为指定的文件路径
boolean renameTo = file.renameTo(file3);
System.out.println("\nfile.renameTo(file3)是否执行成功:"+renameTo+"\n");
System.out.println("执行file.renameTo(file3)后,E:\\\\Java_IOStreamPath\\\\Fold目录下的内容:");
String[] list2 = file2.list();
for (String string : list2) {
System.out.println(string);
}
}
}
3.File类的判断功能
- boolean isDirectory() 判断是否是文件目录
- boolean ifFile() 判断是否是文件
- boolean exists() 判断是否存在
- boolean canRead() 判断是否可读
- boolean canWrite() 判断是否可写
- boolean isHidden 判断是否隐藏
代码举例:
import java.io.File;
import java.util.Arrays;
import javax.swing.text.AsyncBoxView.ChildState;
public class FileClass_use {
public static void main(String[] args) {
FileClass_use fu = new FileClass_use();
fu.test();
}
public void test() {
File file = new File("E:\\Java_IOStreamPath\\Fold\\test2.txt");
// boolean isDirectory() 判断是否是文件目录
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold\\\\test2.txt是否是目录:"+file.isDirectory()+"\n");
// boolean ifFile() 判断是否是文件
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold\\\\test2.txt是否是文件:"+file.isFile()+"\n");
// boolean exists() 判断是否存在
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold\\\\test2.txt是否是存在:"+file.exists()+"\n");
// boolean canRead() 判断是否可读
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold\\\\test2.txt是否是可读:"+file.canRead()+"\n");
// boolean canWrite() 判断是否可写
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold\\\\test2.txt是否是可写:"+file.canWrite()+"\n");
// boolean isHidden 判断是否隐藏
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold\\\\test2.txt是否是隐藏:"+file.isHidden()+"\n");
}
}
4.File类的创建、删除功能
- boolean createNewFile()
创建文件,若文件不存在则不创建,并返回False - boolean mkdir() 创建文件目录
创建目录,存在则不创建;若上级目录不存在,也不创建 - boolean mkdirs()
创建目录,存在则不创建;若上级目录不存在,那么一同创建 - boolean delete()
删除文件或文件夹
代码举例:
import java.io.File;
import javax.swing.text.AsyncBoxView.ChildState;
public class FileClass_use {
public static void main(String[] args) {
FileClass_use fu = new FileClass_use();
try {
fu.test();
} catch (IOException e) {
e.printStackTrace();
}
}
public void test() throws IOException {
File file = new File("E:\\Java_IOStreamPath\\Fold\\test3.txt");
File file2 = new File("E:\\Java_IOStreamPath\\Folds");
File file3 = new File("E:\\Java_IOStreamPath\\FoldTest\\fold");
// boolean createNewFile() 创建文件,若文件不存在则不创建,并返回False
if(!file.exists()) {
file.createNewFile();
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold\\\\test3.txt创建成功!\n");
}else {
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold\\\\test3.txt已存在!\n");
}
// boolean mkdir() 创建目录,存在则不创建;若上级目录不存在,也不创建
if(!file2.exists()) {
file2.mkdir();
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold2创建成功!\n");
}else {
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold2已存在!\n");
}
// boolean mkdirs() 创建目录,存在则不创建;若上级目录不存在,那么一同创建
if(!file3.exists()) {
file3.mkdirs();
System.out.println("E:\\\\Java_IOStreamPath\\\\FoldTest\\\\fold创建成功!\n");
}else {
System.out.println("E:\\\\Java_IOStreamPath\\\\FoldTest\\\\fold已存在!\n");
}
// boolean delete() 删除文件或文件夹
if(file2.exists()) {
file2.delete();
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold2删除成功!");
}else {
System.out.println("E:\\\\Java_IOStreamPath\\\\Fold2不存在!");
}
}
}
二次执行: