- File对象代表:文件和目录路径名的抽象表示形式。
—— 一个File对象有可能对应的是目录,也可能对应的是文件(File只是一个路径名的抽象表示形式) - File类和四大家族没有关系,所以File类不能完成文件的读和写。
- File类中常用的方法:
- booleanexists():判断此抽象路径名表示的文件或目录是否存在。
- booleancreateNewFile():如果不存在具有此抽象路径名指定名称的文件时,创建一个新的空文件
- booleanmkdir():创建此抽象路径名指定的目录。
- booleanmkdirs():创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
- StringgetParent():获得此抽象路径名的父目录路径名;如果此路径名没有指定父目录,则返回 null。
- FilegetParentFile():获得此抽象路径名的父目录抽象路径名;如果此路径名没有指定父目录,则返回 null。
- StringgetAbsolutePath():获得此抽象路径名的绝对路径名字符串。
- StringgetName():获得此抽象路径名表示的文件或目录的名称。
- booleandelete():删除此抽象路径名表示的文件或目录。
- booleanisDirectory():判断此抽象路径名表示的文件是否是一个目录。
- booleanisFile():判断此抽象路径名表示的文件是否是一个标准文件。
- longlastModified():获得此抽象路径名表示的文件最后一次被修改的时间。
- longlength():获得此抽象路径名表示的文件的大小。
- booleanrenameTo(File dest):重命名此抽象路径名表示的文件。
- File[]listFiles():获得当前抽象路径名目录中的所有文件/目录,以File数组返回。
public class FileClassTest01 { public static void main(String[] args) { File file = new File("D:/tempFile"); File file1 = new File("D:/File"); File file2 = new File("D:/File/Test/ErDong"); //1.exists():判断文件/目录是否存在 System.out.println(file.exists());//false //2.createNewFile():如果当前目录中不存在指定文件/目录,创建一个新文件 if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } //3.mkdir():如果当前目录中不存在指定文件/目录,创建一个新的目录 if (!file1.exists()) { file1.mkdir(); } //4.mkdirs():如果当前目录中不存在指定文件/目录,创建一个新的目录,包括所有必需但不存在的父目录 if (!file2.exists()) { file2.mkdirs(); } //5.getParent:获得此抽象路径名文件的父目录路径 System.out.println(file2.getParent());//D:FileTest File parentFile = file2.getParentFile(); System.out.println(parentFile.getAbsolutePath());//D:FileTest //8.getName():获得此抽象路径名表示的文件/目录名称 System.out.println(file2.getName());//ErDong //9.delete():删除 file2.delete(); if (!file2.exists()) { file2.mkdirs(); } //10.isDirectory:判断抽象路径名表示的文件是否为一个目录 System.out.println(file2.isDirectory());//true //11.isFile:判断抽象路径名表示的文件是否为一个标准文件 System.out.println(file.isFile());//true //12.lastModified():获得抽象路径名文件最后一次修改的时间 long l = file1.lastModified(); Date date = new Date(l);//new一个日期对象 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");//设置日期格式 System.out.println(sdf.format(date));//2021-11-04 16:24:34 846 //13.length():获得抽象路径名文件的大小 System.out.println(file.length());//487 //14.renameTo():重命名 file.renameTo(new File("D:/TempFile")); //15.listFiles():获得当前抽象路径名目录中的所有文件/目录 File file3 = new File("D:"); if (file3.isDirectory()) { System.out.println("进行了判断后得出:"); File[] files = file3.listFiles(); for (File f : files) { System.out.println(f.getAbsolutePath()); } } } }
二、练习-CopyAll
public class CopyAllTest {
public static void main(String[] args) {
//拷贝源
File srcFile = new File("D:\资料\JetBrains 2020.3.x 最新版本全家桶激活 3月21日更新");
//复制目的地
File destFile = new File("E:\test0000test");
//调用复制方法
copyAll(srcFile, destFile);
}
private static void copyAll(File srcFile, File destFile) {
//如果拷贝源是文件
if (srcFile.isFile()) {
//进行拷贝操作后结束方法
FileInputStream in = null;
FileOutputStream out = null;
try {
//创建输入输出流
in = new FileInputStream(srcFile.getAbsolutePath());
//获得输出位置的路径
String outPath = (destFile.getAbsolutePath().endsWith("\") ?
destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\") +
srcFile.getAbsolutePath().substring(3);
out = new FileOutputStream(outPath);
//创建缓冲的byte数组
byte[] bytes = new byte[1024 * 1024];
int readCount = 0;
while ((readCount = in.read(bytes)) != -1) {
out.write(bytes, 0, readCount);
}
//刷新输出
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
//如果拷贝源是目录,创建其目录
//需创建的复制目的地目录的绝对路径
String destPath = (destFile.getAbsolutePath().endsWith("\") ?//判断是否是以"\"结尾
destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\") +
srcFile.getAbsolutePath().substring(3);//截取字符串下标3开始往后的字符串
//创建目录在复制目的地的抽象路径
File newFile = new File(destPath);
//判断目录在复制目的地是否存在,不存在创建
if (!newFile.exists()) {
//创建目录
newFile.mkdirs();
}
//获得其源文件的子文件/目录
File[] files = srcFile.listFiles();
for (File file : files) {
//把子文件/目录拷贝
copyAll(file,destFile);
}
}
}



