file的基本使用:
File f1 = new File("1.jpg");
File f2 = new File("C:\\myPicture\\1.jpg");
System.out.println("f1----"+f1);
System.out.println("f2----"+f2);
String absolutePath = f1.getAbsolutePath();
String absolutePath2 = f2.getAbsolutePath();
System.out.println("f1-absolutePath:---"+absolutePath);
System.out.println("f2-absolutePath:---"+absolutePath2);
String path = f1.getPath();
String path2 = f2.getPath();
System.out.println("f1path:---"+path);
System.out.println("f2path:---"+path2);
String f1Name = f1.getName();
String f2Name = f2.getName();
System.out.println("f1Name---"+f1Name);
System.out.println("f2Name---"+f2Name);
long f1Length = f1.length();
long f2Length = f2.length();
System.out.println("f1Length---"+f1Length);
System.out.println("f2Length---"+f2Length);
=========================================================================
f1----1.jpg
f2----C:\myPicture\1.jpg
f1-absolutePath:---D:\work\workspace\summba-site-serv-website\1.jpg
f2-absolutePath:---C:\myPicture\1.jpg
f1path:---1.jpg
f2path:---C:\myPicture\1.jpg
f1Name---1.jpg
f2Name---1.jpg
f1Length---0
f2Length---29965
=========================================================================
createNewFile()? boolean 創(chuàng)建文件
delete() boolean 執(zhí)行刪除筹燕,如果是文件直接刪除(不去回收站犁苏,永久刪除)舀凛,如果是文件夾寺董,必須是空文件夾才能被刪除,
exists() boolean
isDirctory() boolean
isFile() boolean
mkdir() boolean 創(chuàng)建單級(jí)文件夾
mkdirs()boolean 創(chuàng)建多級(jí)文件夾
File f1 = new File("C:\\myPicture\\1.jpg");//本身存在1.jpg
File f2 = new File("C:\\myPicture\\a.jpg");//本身不存在a.jpg
try {
boolean f1createNewFile = f1.createNewFile();
boolean f2createNewFile = f2.createNewFile();
System.out.println(f1createNewFile);//false 不執(zhí)行創(chuàng)建外驱。本身已有不再創(chuàng)建
System.out.println(f2createNewFile);//true 執(zhí)行創(chuàng)建育灸。本身不存在就創(chuàng)建
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
=========================================================================
list() 得到的是該文件夾下面的所有文件及目錄名
File f1dir = new File("C:\\myPicture");
String[] fidirListNames = f1dir.list();
for (String name : fidirListNames) {
System.out.println(name);
}
1.jpg
2.jpg
3.jpg
a.jpg
wenjianjia
=========================================================================
listFiles()得到的是該目錄下的所有目錄及文件對(duì)象
File[] listFiles = f1dir.listFiles();
for (File file : listFiles) {
System.out.println(file);
}
C:\myPicture\1.jpg
C:\myPicture\2.jpg
C:\myPicture\3.jpg
C:\myPicture\a.jpg
C:\myPicture\wenjianjia
=========================================================================
注意:在獲取指定目錄下的文件或者文件夾時(shí)必須滿足下面兩個(gè)條件
1,指定的目錄必須是存在的昵宇,
2磅崭,指定的必須是目錄。否則容易引發(fā)返回?cái)?shù)組為null瓦哎,出現(xiàn)NullPointerException
文件過(guò)濾器:篩選自己想要的文件砸喻。自定義篩選條件
File f1dir = new File("C:\\myPicture");
File[] listFiles = f1dir.listFiles(new MyFileFilter());
for (File file : listFiles) {
System.out.println(file);
}
1.根據(jù)文件名過(guò)濾
public classMyFileFilterimplementsFilenameFilter{
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".txt");
}
}//C:\myPicture\1.txt
2.篩選出文件夾/文件
public class MyFileFilter implementsFileFilter{
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.isDirectory();//篩選出文件夾
//return pathname.isFile()篩選出文件
}
}