创建和删除
File file1 = new File("不存在的文件.txt");
System.out.println(file1.exists() + "--" + file1.createNewFile());
System.out.println(file1.exists() + "--" + file1.createNewFile() + "\n");
File dir1 = new File("e:/a/aa");
File dir2 = new File("e:/a/bb");
System.out.println(dir1.exists() + "--" + dir1.mkdir());
System.out.println(dir2.exists() + "--" + dir2.mkdirs());
System.out.println(dir1.exists() + "--" + dir1.mkdir());
File dirPath = new File("e:/不存在的目录/b");
if (!dirPath.exists()) dirPath.mkdirs();
File file = new File(dirPath, "demo.txt");
System.out.println("创建带目录的文件:" + file.createNewFile() + "\n");
System.out.println(dirPath.list().length);
System.out.println(dirPath.delete());
System.out.println(file.delete() + "--" + dirPath.delete());
System.out.println(dirPath.exists() + "--" + new File("e:/不存在的目录").exists());
get和判断
public static void main(String[] args) throws IOException {
File file1 = new File("a.txt");
System.out.println(file1.length());
System.out.println(file1.getName());
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getParent() + "\n");
File file2 = new File("e:\\demo.java");
System.out.println(file2.length());
System.out.println(file2.getName());
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getParent() + "\n");
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
System.out.println(dateFormat.format(new Date(file2.lastModified())) + "\n");
System.out.println(file1.exists() + "--" + file1.createNewFile() + "--" + file1.exists());
System.out.println(file1.isFile() + "--" + file1.isDirectory());
System.out.println("重命名:" + file1.renameTo(new File("e:/demo3.txt")));
file1 = new File("e:/aa\\bbb");
System.out.println(file1.mkdirs() + "--" + file1.isFile() + "--" + file1.isDirectory());
}
目录相关操作
public class Test {
public static void main(String[] args) throws IOException {
System.out.println("*********************************************************************************获取系统【根目录】名称列表");
File[] fileRoots = File.listRoots();
System.out.println(Arrays.toString(fileRoots));
System.out.println("*********************************************************************************获取某一【磁盘】的容量信息");
File myFile = new File("e:/");
System.out.println(formatDataSize(myFile.getFreeSpace()));
System.out.println(formatDataSize(myFile.getTotalSpace()));
System.out.println(formatDataSize(myFile.getUsableSpace()));
System.out.println("***************************************************************************获取指定目录下的【文件名称列表】");
String[] fileNames = myFile.list();
System.out.println(fileNames.length);
System.out.println("****************************************************************获取指定目录下后缀名为.txt的【文件名称列表】");
String[] fileNames2 = myFile.list(new MyFilenameFilter(".txt", 0));
for (String name : fileNames2) {
System.out.println(name);
}
System.out.println("******************************************************获取指定目录下大于100Kb、后缀名为.mp3的【文件列表】");
File[] files = myFile.listFiles(new MyFilenameFilter(".mp3", 1024 * 100));
for (File f : files) {
System.out.println(f);
}
}
public static String formatDataSize(long size) {
DecimalFormat formater = new DecimalFormat("####.00");
if (size < 1024) return size + "bytes";
else if (size < Math.pow(1024, 2)) return formater.format(size * Math.pow(1024, -1)) + "KB";
else if (size < Math.pow(1024, 3)) return formater.format(size * Math.pow(1024, -2)) + "MB";
else if (size < Math.pow(1024, 4)) return formater.format(size * Math.pow(1024, -3)) + "GB";
else if (size < Math.pow(1024, 5)) return formater.format(size * Math.pow(1024, -4)) + "TB";
else return "啥玩意啊,有这么大的硬盘吗?";
}
public static class MyFilenameFilter implements FilenameFilter {
private String type;
private long size;
public MyFilenameFilter(String type, long size) {
this.type = type;
this.size = size;
}
public boolean accept(File dir, String name) {
long fileSize = new File(dir, name).length();
return fileSize >= size && name.endsWith(type);
}
}
}
获取指定目录下的全部文件
public static void main(String[] args) {
List<String> filesNames = new ArrayList<String>();
getDirFilesNames("E:/KuGou", filesNames, null);
List<File> files = new ArrayList<File>();
getDirFiles("E:/baiqiantao/doudou/src", null, true, files);
for (String name : filesNames) {
System.out.println(name);
}
for (File file : files) {
System.out.println(file.getName() + "--" + file.length());
}
}
public static int getDirFiles(String dirPath, FilenameFilter filter, boolean isContainDir, List<File> fileList) {
File dir = new File(dirPath);
if (!dir.exists() || !dir.isDirectory()) throw new RuntimeException("目录不存在或不是一个目录");
if (fileList == null) throw new RuntimeException("指定的集合不存在");
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
if (isContainDir) {
fileList.add(file);
}
getDirFiles(file.getAbsolutePath(), filter, isContainDir, fileList);
} else {
if (filter == null || filter.accept(dir, file.getName())) {
fileList.add(file);
}
}
}
return fileList.size();
}
public static int getDirFilesNames(String dirPath, List<String> filePathList, FilenameFilter filter) {
return getDirFormatFilesNames(dirPath, filePathList, filter, true, true, true, 0, 0);
}
private static int getDirFormatFilesNames(String dirPath, List<String> filePathList, FilenameFilter filter, boolean isContainDir, boolean isFormat,
boolean isShowNum, int maxLeval, int currentLvel) {
File dir = new File(dirPath);
if (!dir.exists() || !dir.isDirectory()) throw new RuntimeException("目录不存在或不是一个目录");
if (filePathList == null) throw new RuntimeException("指定的集合不存在");
if (0 == maxLeval || currentLvel < maxLeval) {
currentLvel++;
File[] files = dir.listFiles();
String filePath;
for (int x = 0; x < files.length; x++) {
if (isFormat) {
if (isShowNum) filePath = getSpace(currentLvel) + currentLvel + "." + (x + 1) + ". " + files[x].getAbsolutePath();
else filePath = getSpace(currentLvel) + files[x].getAbsolutePath();
} else {
filePath = files[x].getAbsolutePath();
}
if (files[x].isDirectory()) {
if (isContainDir) {
if (isFormat) {
filePath = filePath + "\t\t★★★★★ 目录 ★★★★★";
}
filePathList.add(filePath);
}
getDirFormatFilesNames(files[x].getAbsolutePath(), filePathList, filter, isContainDir, isFormat, isShowNum, maxLeval, currentLvel);
} else {
if (filter == null || filter.accept(dir, files[x].getName())) {
filePathList.add(filePath);
}
}
}
}
return filePathList.size();
}
private static String getSpace(int level) {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < level; x++) {
sb.append("| _ _ ");
}
return sb.toString();
}
删除指定目录下的全部文件
public static void deleateDirFiles(String dirPath, boolean isDeleateDir) {
File dir = new File(dirPath);
if (dir.isDirectory()) {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
deleateDirFiles(file.getAbsolutePath(), isDeleateDir);
} else {
System.out.println("文件【" + file + "】删除结果:" + file.delete());
}
}
System.out.println(" ★★★★★ 目录【" + dir.getAbsolutePath() + "】中的文件已全部删除");
if (isDeleateDir) {
System.out.println(" ★★★★★ 目录【" + dir + "】删除结果:" + dir.delete());
}
} else {
System.out.println("删除结果:" + dir.delete());
}
}