JAVA學習第五十三課 — IO流(七)File對象練習 & Properties集合,iofile
一、練習
深度遍曆檔案夾
深度遍曆很自然而然想到遞迴,而遞迴就很自然的想到其實現的底層演算法是棧
對指定目錄下列出所有內容(包含子目錄的內容)
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流把一個檔案全部讀取出來
public static void main(String[] args)throws Exception
{
String path="d:\\aa.txt";
File file=new File(path);
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis,"utf-8");
BufferedReader br=new BufferedReader(isr);
StringBuffer line=new StringBuffer();
String str=null;
File file1=new File("d:\\cc.txt");
FileOutputStream fos=new FileOutputStream(file1);
OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
BufferedWriter bw=new BufferedWriter(osw);
while((str=br.readLine())!=null){
line.append(str);
line.append("\n");
}
System.out.println(line);
bw.flush();
bw.close();
}
javaiofile對象中擷取檔案長度時 調用length方法 返回的是檔案佔用空間大小怎擷取檔案自身的大小?
返回的是檔案中含有的位元組數。