JAVA學習課第五十三屆 — IO流程(七)File打靶 & Properties設定

來源:互聯網
上載者:User

標籤:

一個、鍛煉

深度遍曆目錄

深度遍曆非常自然而然想到遞迴,而遞迴就非常自然的想到事實上現的底層演算法是棧

對指定檔案夾下列出全部內容(包括子檔案夾的內容)

PS:建議不要遍曆C盤

import java.io.*;public class Main {public static void main(String[] args) throws IOException {File dir = new File("D:\\ACM集訓");ListAllDemo(dir,0);}public static void ListAllDemo(File dir,int level) throws IOException {System.out.println(GetSpace(level)+"檔案夾 : "+dir.getAbsolutePath());level++;//為了縮排//擷取指定檔案夾下當前全部檔案/檔案對象File[] files = dir.listFiles();for(int i =0;i<files.length;i++){if(files[i].isDirectory()){ListAllDemo(files[i],level);}else{System.out.println(GetSpace(level)+"檔案 : "+files[i].getAbsolutePath());}}}public static String GetSpace(int level) {// TODO Auto-generated method stubStringBuilder sb = new StringBuilder();for(int i = 0;i<level;i++)sb.append("    ");return sb.toString();}}

刪除檔案夾

import java.io.*;public class Main {public static void main(String[] args) throws IOException {File dir = new File("D:\\ACM集訓1");//dir.delete(dir);假設檔案中有內容,是不能從根資料夾刪除的,必須從裡往外刪DeleteDemo(dir);}public static void DeleteDemo(File dir) {File[] files = dir.listFiles();for(File f: files){if(f.isDirectory()){DeleteDemo(f);}else{System.out.println(f.getAbsolutePath()+" : "+f.delete());//刪除檔案}}System.out.println(dir.getAbsolutePath()+":"+dir.delete());//刪除檔案夾}}

二、Properties集合

API文檔解釋:Properties 類表示了一個持久的屬性集。Properties 可儲存在流中或從流中載入。

屬性列表中每一個鍵及其相應值都是一個字串。


特點:

該集合中的鍵和值都是字串類型

集合中的資料能夠儲存到流中,或從流中擷取

通常該集合用於操作以鍵值對的形式存在的設定檔

import java.io.*;import java.util.Properties;import java.util.Set;public class Main {public static void main(String[] args) throws IOException {PropertiesDemo();}public static void PropertiesDemo() {//存Properties pro = new Properties();pro.setProperty("a", "1");pro.setProperty("b", "2");pro.setProperty("c", "3");pro.setProperty("d", "1");//改動pro.setProperty("c", "6");//取Set<String> name = pro.stringPropertyNames();//stringPropertyNames():返回此屬性列表中的鍵集。當中該鍵及其相應值是字串,//假設在主屬性列表中未找到同名的鍵,則還包含預設屬性列表中不同的鍵。for(String s : name){String value = pro.getProperty(s);//getProperty():用指定的鍵在此屬性列表中搜尋屬性。System.out.println(s+" : "+value);}}}

三、Properties集合和流對象相結合

1.list方法:

將屬性列表輸出到指定的輸出資料流。

此方法對調試非常實用。


public static void PropertiesDemo() {Properties pro = new Properties();pro.setProperty("a", "1");pro.setProperty("b", "2");pro.setProperty("c", "3");pro.setProperty("d", "1");pro.list(System.out);}

調用此方法隨時都能夠看到Properties集合儲存的是什麼

和 System.getProperties(),差點兒相同


2.store方法

這方法就體現了Properties集合特點中的持久化,將集合中的資訊儲存起來

public void store(OutputStream out, String comments)throws IOException
以適合使用 load(InputStream) 方法載入到 Properties 表中的格式。將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出資料流。 

out - 輸出資料流。

comments - 屬性列表的描寫敘述。


而save方法已經過時了。

public static void PropertiesDemo() throws IOException {Properties pro = new Properties();pro.setProperty("a", "1");pro.setProperty("b", "2");pro.setProperty("c", "3");pro.setProperty("d", "1");//將Properties集合中的資訊持久化的儲存到檔案裡,須要關聯輸出資料流FileOutputStream fos = new FileOutputStream("tep.txt");//集合資料存放區到檔案裡。storepro.store(fos, "name and age");//這種方法不建議是使用中文資訊}

3.load方法

load(InputStream inStream) 輸入資料流中讀取屬性列表(鍵和元素對)。

public static void PropertiesDemo() throws IOException {//集合中的資料來自檔案,而不是我們存放的Properties pro = new Properties();//注意必須保證檔案裡的資料是索引值對FileInputStream fis = new FileInputStream("tep.txt");//使用load方法pro.load(fis);pro.list(System.out);}

其原理

public static void Myload() throws IOException {Properties pro = new Properties();BufferedReader br = new BufferedReader(new FileReader("tep.txt"));String str = null;while((str = br.readLine())!=null){if(str.startsWith("#"))continue;//由於檔案裡有些配置資訊不是含“=”的String[] arr = str.split("=");//System.out.println(arr[0]+":"+arr[1]);pro.setProperty(arr[0], arr[1]);}pro.list(System.out);br.close();}

4.改動已有的設定檔裡的資訊

1.讀取這個檔案

2.將文字中的鍵值資訊儲存到集合中

3.通過集合改動資訊

4.在通過流將改動後的資訊儲存到檔案裡

public static void PropertiesDemo() throws IOException{File file = new File("tep.txt");if(!file.isFile()){file.createNewFile();}FileReader fr = new FileReader("tep.txt");//FileWriter fw = new FileWriter(file);假設放在這。就會建立一個檔案覆蓋file。最後檔案存的c=3Properties pro = new Properties();pro.load(fr);//pro.list(System.out);pro.setProperty("c", "3");//一定要改完後才關聯輸出資料流對象,不要在上面就關聯FileWriter fw = new FileWriter(file);//流能夠直接操作File對象pro.store(fw, "after change");}

四、練習

擷取一個應用程式的使用次數,超過3次,給出使用次數已到請注冊的資訊,並不要再執行程式的資訊

分析:

此需求。須要一個計數器,每次程式啟動計數器進記憶體。次數+1,退出程式,計數器關閉。儲存到檔案。

由於信心要明白。應該有名字和次數。->鍵值對->Map->Map+IO -> Properties

public static void PropertiesDemo() throws IOException{File countFile = new File("conutFile.properties");//索引值對的配置資訊(java)if(!countFile.isFile())countFile.createNewFile();FileInputStream  fis = new FileInputStream(countFile);Properties pro = new Properties();pro.load(fis);//從集合中通過鍵擷取使用次數String value = pro.getProperty("time");int count = 0;//定義計數器if(value!=null){count = Integer.parseInt(value);//轉換成intif(count>=3){throw new RuntimeException("使用次數已到。請注冊!");}}count++;//將改變後的資料又一次儲存pro.setProperty("time", count+"");//改動FileOutputStream fos = new FileOutputStream(countFile);//關聯輸出資料流pro.store(fos, "time is");//儲存到檔案fos.close();fis.close();}


在開發的時候就能夠將這段代碼封裝成一個對象,在執行時,建立就可以




著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

JAVA學習課第五十三屆 — IO流程(七)File打靶 &amp; Properties設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.