黑馬程序員_day24_File類、過濾器和遞歸、properties類

----------- android培訓java培訓、java學習型技術博客、期待與您交流! ------------

一、File類:

專門用於描述系統中文件或者文件夾的對象。 

可以用於操作文件或者文件夾的屬性信息。

1,獲取文件信息。

獲取名稱,

獲取路徑。

獲取大小。

獲取時間。

...

2,判斷。

是隻讀的不?

是隱藏的不?

3,文件的創建和刪除以及該文件是否存在,文件對象自己最清楚。

具體創建,刪除,是否存在等功能。

 File中構造時,指定的路徑可以是存在的,也可以是不存在的。 

//c盤下的demo.txt封裝成file對象。

File f1 = new File("c:\\demo.txt");

//c盤下的abc文件夾封裝成file對象。

File f2 = new File("c:\\abc");

File f5 = new File("c:\\abc\\ahha\\a.txt");//windows上使用。

File f6 = new File("c:"+ File.separator+"abc"+ File.separator+"ahha"+ File.separator+"a.txt");//可以在任何平臺都可以使用。 File.separator是名稱分隔符,在windows系統上是雙反斜槓\\

private static final String NAME_SEPARATOR = File.separator;也可以這樣自定義。

二、file類方法

1、獲取名稱

File file = new File("abc");

String name = file.getName();//獲取名稱

String absPath = file.getAbsolutePath();//獲取絕對路徑。無論file中封 裝的是什麼,獲取到的都是絕對的。全路徑。 

String path = file.getPath();//獲取路徑。獲取的是file中封裝的內容。 

String dir = file.getParent();//獲取父目錄。

2、大小

long len = file.length();

long len2 = file.getTotalSpace();獲取文件所在盤符的空間大小

3、獲取最後一次修改的時間

long time = file.lastModified();

//爲了知道具體的時間,需要進行轉換。

//1,time毫秒值轉成Date對象。//2,Date對象進行格式化。 

Date d = new Date(time);

DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);

String str_time = format.format(d);

System.out.println(str_time);

4、判斷

isHidden()是不是隱藏的

isDirectory()是不是一個目錄

5、創建和刪除文件和文件夾

對於文件的創建和刪除

File file = new File("c:\\2.txt");

//1,創建。

boolean b = file.createNewFile();

如果文件不存在,就創建。如果文件存在,就不創建。輸出流流會覆蓋,如果不覆蓋,需要在構造函數中加入true參數。續寫。

//2,刪除。

boolean  b = file.delete();

//3,是否存在。

boolean b = file.exists();

對於文件夾的創建和刪除

創建文件夾。

File dir = new File("c:\\abc");

boolean b = dir.mkdir();創建一級文件夾

File dir = new File("c:\\abc\\a\\b\\c");

boolean b = dir.mkdirs();創建多級文件夾

刪除文件夾

boolean b = dir.delete();從裏往外刪,刪完內容才能刪目錄

要判斷file中封裝的是文件還是目錄必須有前提,必須存在。用exists進行判斷。 

//if(file.exists() && file.isFile())

6、虛擬機退出時再刪除deleteOnExit()

File f1 = new File("c:\\haha.jpg");

// file.deleteOnExit();虛擬機退出時會自動刪除f1文件,因爲如下

//流對象正在操作這個文件。。

//file.delete();//此時就會發生了異常

7、對文件進行重命名

File f1 = new File("c:\\haha.jpg");

File f2 = new File("d:\\xixi.jpg");

f1.renameTo(f2);haha.jpg更名爲xixi.jpg並從c盤移到d盤。 有一個剪切動作。

8、列出根目錄 系統的盤符

File[] roots = File.listRoots();列出系統根

for(File root : roots){對盤符進行遍歷

System.out.println(root);

}

三、過濾器

File類中的list()方法 返回的是字符串數組。

String[] names = file.list();//將指定目的下的文件或者文件夾的名稱存儲到字符串數組中,包含隱藏文件。

list(FilenameFilter  filter)過濾出滿足過濾器的文件和目錄的路徑字符串存儲到字符串數組中並返回

FilenameFilter是一個接口。此接口的實例可用於文件名過濾器

代碼如下:

public class FilterByJavaFile implements FilenameFilter {

public boolean accept(File dir, String name) {

return name.endsWith(".java");返回以.java結尾的文件名

}

}

listFiles()方法 返回的是文件對象數組。將目錄下的文件或文件夾都封裝成文件對象。

listFiles(FileFilter filter)文件過濾器

將隱藏的文件過濾出來

File[] files = file.listFiles(new FilterByHidden());

for(File f : files){

System.out.println(f);

}

}

隱藏過濾器

public class FilterByHidden implements FileFilter {

public boolean accept(File pathname) {

return pathname.isHidden();

}

}

可以更改結尾條件的過濾器

public class FilterBySuffix implements FilenameFilter {

private String suffix;

public FilterBySuffix(String suffix) {

super();

this.suffix = suffix;

}

public boolean accept(File dir, String name) {

return name.endsWith(suffix);

}

}

四、遞歸

遞歸就是函數自身調用自身(直接或者間接)

1,一定要定義條件。 否則就是StackOverflowError棧內存溢出

 2,一定要注意遞歸次數。不宜過多,否則也會棧內存溢出

 什麼時候用遞歸呢?

當一個功能被複用,每次這個功能需要的參數都是根據上一次該功能的元素得出的。

練習:刪除一個帶內容的文件夾。

思路:

1,刪除一個帶內容的目錄,必須按照window的規則,從裏往外刪。

2,要刪除最裏面,如果做到的呢?可以使用遞歸。

public static void main(String[] args) {

File dir = new File("e:\\demodir");

removeDir(dir);

}

public static void removeDir(File dir){

File[] files = dir.listFiles();

for(File file : files){

if(file.isDirectory()){

removeDir(file);

}else{

System.out.println(file+":"+file.delete());

}

}

System.out.println(dir+":"+dir.delete());

}

}
五、properties類(重要)

Properties:

1, Map接口中Hashtable的子類。

2, 該類上沒有泛型定義,因爲它的鍵值都是固定的字符串類型。

3, 因爲存儲都是字符串數據,通常都作爲屬性信息存在。

4, 該集合最大的特點就是可以和IO技術相結合。也就是,該集合中的數據可以來自於流。也可以將集合中的數據寫入到流中。

Properties集合最基本演示。

public static void methodDemo_1(){

//1,創建Properties集合對象。

Properties prop = new Properties();

//2,存儲元素。

prop.setProperty("wangwu", "28");

prop.setProperty("zhangsan", "20");

prop.setProperty("lisi", "34");

//取元素。所有。 

Set<String> nameSet = prop.stringPropertyNames();

for(String name : nameSet){

String value = prop.getProperty(name);

System.out.println(name+":"+value);

}

}

}

//使用list方法將集合中的數據指定目的地。如指定目的地是顯示器System.out

prop.list(System.out);//一般用於調試。

下面的方法很重要 開發時常用到

public static void methodDemo_3() throws IOException {

Properties prop = new Properties(); 集合

FileInputStream fis = new FileInputStream("info.txt");輸入流

//將流中的數據存儲到集合中。 使用集合的load方法。

prop.load(fis);

//想要對數據做修改。 

prop.setProperty("wangcai", "37");其實是對文件中的鍵值對進行覆蓋。

//想要將修改的數據保存到文件中。 

// 使用store方法。

FileOutputStream fos = new FileOutputStream("info.txt");新創該文件對其覆蓋。

prop.store(fos, "haha");haha部分是描述部分

fos.close();

fis.close();

prop.list(System.out);

}

六、對properties類的操作練習

練習:定義一個功能用於記錄住軟件運行的次數,如果運行次數大於5次。不要在運行

並給出提示:試用次數已到,請註冊!給錢!

思路:

1,計數器。而且這個計數器必須軟件運行結束後,持久化存儲。

2,每次軟件啓動都要讀取這個記錄了計數器的數據的文件。並將計數器的值取出自增後,在重新存儲。

3,數值需要名稱標記,就有了鍵值對。這就是map集合,還要操作硬盤設備上的數據,就使用到了IO流。

 mapio結合的對象正好有Properties.

代碼如下:

public static void main(String[] args) throws IOException {

if(countDemo()){

//運行程序代碼。

System.out.println("運行程序");

}else{

System.out.println("試用次數已到,請註冊!給錢!");

}

}

public static boolean countDemo() throws IOException{

Properties prop = new Properties();集合

int count = 0;

//配置文件。

File confile = new File("config.txt");

if(!confile.exists())

confile.createNewFile();確認配置文件的存在以便讀入流

FileInputStream fis = new FileInputStream(confile);

//將流中的數據加載到prop中。 

prop.load(fis);

//獲取配置文件中的次數。

String value = prop.getProperty("count");

if(value!=null){

count = Integer.parseInt(value);

if(count>=5){

// System.out.println("試用次數已到,請註冊!給錢!");

return false;

}

}

count++;

System.out.println("運行"+count+"");

//將具體的鍵和次數存儲到集合中。

prop.setProperty("count", String.valueOf(count));//count+"";

//將集合中的數據寫入到文件中持久化。

FileOutputStream fos = new FileOutputStream(confile);

prop.store(fos, "");

fos.close();

fis.close();

return true;

}

}

七、列出文件清單練習

思考題:

對指定目錄中的所有(包含子目錄)Java文件的絕對路徑寫入到一個文本文件中。

這樣查找某個java文件會比較便捷。建立指定文件的清單。

思路:

1,遞歸。

2,在遞歸過程需要過濾。

3,滿足條件的文件很多,需要容器存儲。 

4,將存儲的文件絕對路徑寫入到一個文件中。

 定義功能對指定的目錄進行遞歸。

 在遞歸過程中需要過濾。

 而且對過濾條件滿足需要進行存儲。

public static void listAllJavaFile(File dir, FilenameFilter filter,List<File> list){

File[] files = dir.listFiles();

if(files!=null){

for(File file : files){

if(file.isDirectory()){//如果是目錄就遞歸。 

listAllJavaFile(file, filter, list);

}else{

if(filter.accept(dir, file.getName())){//使用過濾器對指定的目的和文件名進行過濾。符合條件進行存儲。

list.add(file);

}

}

}

}

}

對集合中存儲的內容的信息寫入到文件中。

public static void write2File(List<File> list,File dest) throws IOException{

BufferedWriter bufw = null;

try {

bufw = new BufferedWriter(new FileWriter(dest));

//遍歷集合。 

for (File file : list) {

bufw.write(list.getAbsolutePath());

bufw.newLine();

bufw.flush();

}

} finally{

if(bufw!=null)

try {

bufw.close();

} catch (IOException e) {

throw new RuntimeException("關閉失敗");

}

}

}

----------- android培訓java培訓、java學習型技術博客、期待與您交流! ------------

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章