java.io.File 是操作文件夹及文件的类,具有跨平台的特性
Constructor
File(String pathname)
Creates a new File
instance by the given pathname string
FIle(File parent, String child)
Creates a new File
instance from a parent abstract pathname and a child pathname string.
File(String parent, String child)
Creates a new File
instance from a parent pathname string and a child pathname string.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
File file = new File("E:/a.txt"); File parent = new File("E:/"); String child = "a.txt"; file = new File(parent, child); String parentStr = "E:/"; file = new File(parentStr, child); System.out.println(file.pathSeparator); // ; 配置环境变量的分隔符 inux 下为 : System.out.println(file.separator); // 路径分隔符 linux下为/ System.out.println(file); // E:a.txt
Method
boolean canWrite(); // canExective() canRead()
Tests whether the application can read the file
boolean exists()
Tests whether the file(directory) is exists
boolean createNewFile()
only can creat the file not directory, if the file has exists, do nothing
boolean delete()
Deletes the file or directory
String getName()
Returns the name of the file or directory
File getAbsoluteFile()
Returns the absolute form of this abstract pathname
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
File file = new File("E:/a.txt"); boolean b = file.exists(); // 判断文件/文件夹是否存在 b = file.canExecute(); // 判断文件/文件夹是否可执行 b = file.canRead(); // 判断文件是否可读 b = file.createNewFile(); // 文件不存在则创建,存在什么都不做 b = file.delete(); // 删除文件/文件夹 File newFile = file.getAbsoluteFile(); // E:a.txt 得到绝对路径对象 String name = file.getName(); // E:a.txt 得到文件名/文件夹名 即最后一级目录
String getParent()
Returns the pathname string of this abstract pathname's parent, or null
if this pathname does not name a parent directory
boolean isDirectory()
Tests whether the file denoted by this abstract pathname is a directory.
long length()
Returns the length of the file
File[] listFiles()
Returns an array of abstract pathnames
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
File file = new File("E:\usefulPY"); String parent = file.getParent(); // E: boolean b = file.isDirectory(); // 判断是否是目录 long l = file.length(); // 获取文件(只能是文件)大小 File[] fileList = file.listFiles(); // 获取此目录下文件夹及文件 // [E:/usefulPY/1.txt,E:/usefulPY/voa.py]
File[] listFiles(FileFilter filter) // FileFilter This is a functional interface
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
File file = new File("E:\usefulPY"); // 匿名内部类实现方法 File[] fileList = file.listFiles(new FileFilter(){ @Override public boolean accept(File pathname) { String path = pathname.getName(); return path.endsWith(".txt"); } }); // 获取此目录下文件夹及文件 [E:/usefulPY/1.txt] // lambda 表达式 File[] fileList = file.listFiles(pathname-> { String path = pathname.getName(); return path.endsWith(".txt"); } );
boolean mkdirs() // can creat muti-level directory
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
boolean renameTo(File file)
boolean b = file.renameTo(new File("E:/b.txt")); // 重命名文件或文件夹 b = file.setWritable(false); // 修改权限 File file = new File("E:/aa/a/a"); b = file.mkdirs(); // E:aaaa
recursive the directory
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) { ArrayList<File> fileList = recursiveFile(new File("E:\officFile")); for(File f:fileList){ System.out.println(f); } } public static ArrayList<File> recursiveFile(File file){ ArrayList<File> fileList = new ArrayList<>(); if(file.exists() == false) return fileList; File[] list = file.listFiles(); for(File f: list) { if(f.isDirectory()){ fileList.addAll(recursiveFile(f)); }else{ fileList.add(f); } } return fileList; } }