Official Documents
java.io File
An abstract representation of the pathnames File and Directory. abstract file and directory pathnames.
For UNIX platforms, the prefix of an absolute pathname is always "/". Relative pathnames have no prefix. The abstract pathname denoting the root directory has the prefix "/" and an empty name sequence.
note:
-
Under the file directory should win: drive letter (D): \ beginning
-
linux under the file directory should be: / beginning
Examples
// 指定目录中创建一个文件
File file1=new File("D:\\白居易工作文件夹/");
File.createTempFile("prefix",".text",file1);
复制代码
测试此抽象路径名表示的文件或目录是否存在。
boolean exists()
创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录。 请注意,如果此操作失败,它可能已成功创建一些必需的父目录。
boolean mkdirs()
复制代码
Files folder inquiry
// 查询填写目录下的全部文件
File file=new File("D:\\白居易工作文件夹/");
String str[] =file.list();
for (String i:str) {
System.out.println(i);
}
复制代码
// 重写 FilenameFilter中的accept中的方法(根据文件名称比较)
public class FilenameList implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
if(dir.getName().equals(name)){
return true;
}else{
return false;
}
// 根据文件后缀过滤
//返回文件类型
// String type = FileTypeUtil.getType(dir);
//name 为文件后缀
// if(type.equals(name)){
// return true;
// }else{
// return false;
// }
}
}
// 把重写过的accept当作过滤器,查询目录下的文件
FilenameList filenameList=new FilenameList();
File [] files =file.listFiles();
for (File ii:files) {
Boolean aBoolean =filenameList.accept(ii,"prefix1252806276750206736.text");
if(aBoolean){
System.out.println(ii.getName());
}
}
复制代码