File 文件操作:
一、常用方法:
方法 | 类型 | 描述 |
---|
public File(String pathname) | 构造 | 给定一个要操作文件的完整路径 |
public File(File parent, String child) | 构造 | 给定要操作文件的父路径和子文件名称 |
public boolean createNewFile() throws IOException | 普通 | 创建文件 |
public boolean delete ( ) | 普通 | 删除文件 |
public boolean exists ( ) | 普通 | 判断给定路径是否存在 |
public File getParentFile() | 普通 | 找到一个指定路劲的父路径 |
public boolean mkdirs ( ) | 普通 | 创建指定目录 |
public boolean canRead() | 普通 | 文件是否能读 |
public boolean canWrite() | 普通 | 文件是否能写 |
public boolean canExecute() | 普通 | 文件是否能执行 |
public long length ( ) | 普通 | 获取文件大小(返回字节长度) |
public long lastModified ( ) | 普通 | 获得最后一次修改日期 |
public boolean isDirectory ( ) | 普通 | 是否是目录 |
public boolean isFile ( ) | 普通 | 是否是文件 |
public boolean isHidden ( ) | 普通 | 是否隐藏 |
public File门 listFiles ( ) | 普通 | 列出目录中的全部文件信息 |
二、求某个磁盘或目录下面的相同文件类型有多少个:
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class FileTest {
public static void main(String[] args) {
File feile=new File("E:"+File.separator+"oraclesqltext"+File.separator);
HashMap map =new HashMap();
filelist(feile,map);
System.out.println(map);
}
public static void filelist(File feile,HashMap map){
HashMap hashMap=map;
if(feile.isDirectory()){
File[] files = feile.listFiles();
if (files!=null){
for (File feiled:files) {
filelist(feiled,hashMap);
}
}
}else{
String name = feile.getName();
String substring = name.substring(name.indexOf(".") + 1);
Object o = hashMap.get(substring);
int sum;
if (o==null){
sum=1;
hashMap.put(substring,sum);
}else{
sum=(int)o;
hashMap.put(substring,sum+1);
}
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2805afdcf8754e8f8dbead9655ed0b08.png#pic_center)