生成File对象的三个方法
1 2 3 4 5 6 7 8 9 10 11 12 13 package File;import java.io.File;public class FilePath { public static void main (String[] args) { method3(); method2(); method1(); } }
File(path)
1 2 3 4 5 private static void method1 () { String path = "p:\\test\\test.txt" ; File file = new File (path); System.out.println(file); }
File(path1,path2)
1 2 3 4 5 6 private static void method2 () { String path1 = "p:\\test" ; String path2 = "test.txt" ; File file = new File (path1,path2); System.out.println(file); }
File(file,path)
1 2 3 4 5 6 private static void method3 () { File file = new File ("p:\\test" ); String path = "test.txt" ; File file1 = new File (file,path); System.out.println(file1); }
创建文件夹/文件
public boolean createNewFile()
:创建一个新的空的文件
public boolean mkdir()
:创建一个单级文件夹
public boolean mkdirs()
:创建一个多级文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 package File;import java.io.File;import java.io.IOException;public class FilePath { public static void main (String[] args) throws IOException { newmkdirs(); createFile(); } }
创建文件夹file.mkdir()
与file.mkdirs()
1 2 3 4 5 6 7 private static void newmkdirs () { File file = new File ("p:\\test\\a\\b\\c" ); boolean results = file.mkdir(); boolean results2 = file.mkdirs(); System.out.println(results); System.out.println(results2); }
创建文件file.createNewFile()
1 2 3 4 5 6 7 private static void createFile () throws IOException { File file = new File ("p:\\test\\a.txt" ); boolean resulte = file.createNewFile(); System.out.println(resulte); }
删除
file.delete()
删除文件和空文件夹
注意点:
不走回收站
如果删除得是文件,则直接删除,如果是文件夹,则只能删除空文件夹
如果想删除有内容的文件夹,则需要先删除文件夹里面的全部内容才能删除该文件夹(用递归删除)
总的来说只能删除文件和空文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 private static void delfile () { File file = new File ("p:\\test\\test.txt" ); boolean results = file.delete(); System.out.println(results); }
文件或者文件夹是否存在
public boolean isDirectory()
:测试此抽象路径名表示的File
是否为目录
public boolean isFile()
:测试此抽象路径名表示的File
是否为文件
public boolean exists()
:测试此抽象路径名表示的File
是否存在
public string getName()
:返回由此抽象路径名表示的文件或目录的名称
public boolean isDirectory()
1 2 3 4 5 6 private static void isdir () { File file = new File ("p:\\test\\a.txt" ); boolean results = file.isDirectory(); System.out.println(results); }
public boolean isFile()
1 2 3 4 5 6 private static void is_File () { File file = new File ("p:\\test" ); boolean results = file.isFile(); System.out.println(results); }
public boolean exists()
1 2 3 4 5 6 private static void is_path () { File file = new File ("p:\\test" ); boolean results = file.exists(); System.out.println(results); }
public string getName()
1 2 3 4 5 6 private static void isPathName () { File file = new File ("p:\\test\\test.txt" ); String results = file.getName(); System.out.println(results); }
获取文件夹内容
file.listFile()
listFile方法注意事项:
当调用者不存在时,返回null
当调用者是一个文件时,返回null
当调用者是一个空文件夹时,返回一个长度为0
的数组
当调用者是一个有内容的文件夹时,将里面的文件和文件夹的路径放在File数组中返回
当调用者是一个有隐藏文件的文件夹时,将里面所有文件和文件夹的路径都放在File数组中返回,并且是包含隐藏内容
1 2 3 4 5 6 7 8 9 10 private static void getDirContent () { File file = new File ("p:\\test\\a.txt" ); File[] files = file.listFiles(); if (files != null ) for (File path:files) { System.out.println(path); } }
练习
练习一:在当前模块下的aaa文件夹中创造一个a.txt文件
方法一
1 2 3 4 5 6 7 8 private static void newDirFile1 () throws IOException { File file = new File ("./aaa/a.txt" ); File newaaa = new File ("./aaa" ); boolean mkdir = newaaa.mkdir(); boolean newFile = file.createNewFile(); System.out.println(newFile); }
方法二
1 2 3 4 5 6 7 8 9 private static void newDirFile2 () throws IOException { File file1 = new File ("./aaa" ); if (!file1.exists()) file1.mkdirs(); File file = new File (file1,"a.txt" ); boolean newFile = file.createNewFile(); System.out.println(newFile); }
练习二:在当前模块新建多级文件夹a/b/c/a.txt,然后再把它删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package File;import java.io.File;import java.io.IOException;public class ListFile { public static void main (String[] args) throws IOException { File file = new File ("./a/b/c" ); if (!file.exists()) file.mkdirs(); File file1 = new File (file,"a.txt" ); file1.createNewFile(); File a = new File ("./a" ); deleteDir(a); } private static void deleteDir (File a) { File[] files = a.listFiles(); for (File file:files) { if (file.isFile()) file.delete(); else deleteDir(file); } a.delete(); } }
练习三:用HashMap集合来统计某文件夹内文件类型的个数
1 2 3 4 5 6 7 private static void getCountFileType () { File file = new File ("p:\\test" ); HashMap<String,Integer> hm = new HashMap <>(); getCount(hm,file); System.out.println(hm); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 private static void getCount (HashMap<String, Integer> hm, File file) { File[] files = file.listFiles(); for (File f : files) { if (f.isFile()){ String fileName = f.getName(); String[] fileNameArr = fileName.split("\\." ); if (fileNameArr.length == 2 ){ String fileEndName = fileNameArr[1 ]; if (hm.containsKey(fileEndName)){ Integer count = hm.get(fileEndName); count++; hm.put(fileEndName,count); }else { hm.put(fileEndName,1 ); } } }else { getCount(hm,f); } } }