File{
作用:將文件/文件夾封裝成對象,可以將一個已存在或不存在的文件或目錄封裝成File對象
常用方法{
boolean creatNewFile() 該文件不存在,就創建該文件,返回true。如果文件存在,就不創建,返回false
boolean mkdir() 創建文件夾(目錄)
boolean delete() 文件(文件夾)刪除,如果文件夾裏有內容的話,要先把內容刪除該文件夾才能刪除
list() 返回一個字符串數組,包含目錄內容,所以對象中封裝的必須是目錄
}
例子
該例子用到遞歸:函數自身直接或間接地調用了自身/** * 對文件夾進行深度遍歷,並用字符串打印出該文件內容 * * 步驟: * 1.獲取一個文件夾對象,遍歷內容 * 2.判斷該文件是一個目錄還是一個文件 * 3.是文件就將其存儲到容器中 * 4.如果還是一個目錄就繼續遍歷 */ public class BianLi { public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("F:\\"); getFilesContent(file, 0); } private static void getFilesContent(File file, int count) { // TODO Auto-generated method stub System.out.println(getSpace(count) + file.getAbsolutePath()); count++; File[] list = file.listFiles(); for(File l : list){ if(l.isDirectory()){ getFilesContent(l, count); }else{ System.out.println(getSpace(count) + l.getAbsolutePath()); } } } private static String getSpace(int count) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); for (int i = 0; i < count; i++) { sb.append(" "); } return sb.toString(); } }
一個功能在被重複使用,並且每次使用時參與的運算結果與上一次調用有關,這時候就使用遞歸
注意:遞歸要明確條件,否則容易棧溢出(死遞歸,即一直在調用函數,不出棧)
}
Properties{
定義:用於操作以鍵值對形式存在的配置文件
特點{
}1.集合中鍵和值都是字符串,所以不需要指定泛型
2.集合中的數據可以保存在流中或從流中獲取
常用方法{
Set stringPropertyNames()返回容器中鍵的信息,用Set集合存儲。該方法是jdk1.6纔出現的
void store(輸出流, 描述)將集合的鍵值對寫入輸出流關聯的文件中,描述信息在文件中以“#”開頭,並且不允許寫中文否則出現亂碼
例子
<pre name="code" class="java"> File file = new File("F:\\a.txt"); BufferedWriter bw = new BufferedWriter( new FileWriter(file)); Properties p = new Properties(); p.put("name1", "21"); p.put("name2", "22"); p.put("name3", "23"); p.put("name4", "24"); p.store(bw, "asdf");
注意:
將輸入流關聯文件的內容傳到集合中(必須保證該文件的數據是以鍵值對形式存在的)
加載的時候會將以“#‘開頭的描述內容屏蔽
例子
File file = new File("F:\\a.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); Properties p = new Properties(); p.load(br); System.out.println(p);
/** * 實現Properties的load方法 * * 步驟: * 1.接收源,容器 * 2.創建緩衝流,接收文件中每一行的內容 * 3.屏蔽以“#”開頭的內容 * 4.用split方法分割每行“=”兩邊的內容 * 5.將分割的內容分別作爲容器的鍵和值存儲 */ public static void myLoad(Reader reader, Properties p) throws IOException{ BufferedReader br = new BufferedReader(reader); String line = null; while((line=br.readLine()) != null){ if(line.startsWith("#")) continue; String[] s = line.split("="); p.put(s[0], s[1]); br.close(); } }
}
小練習{
/** * * 僅允許一個程序運行五次。 * 創建一個文件,內部有一個計數器,運行一次,計數器就自增一次,直至計數器爲五就停止 * * 步驟: * 1.創建一個文件對象,作爲包含計數器的文件 * 2.創建相應的輸入流對象,獲取文件的內容 * 3.創建容器存儲文件內計數器的內容 * 4.修改計數器的值 * 5.創建輸出流將文件中計數器的內容重寫 * @throws IOException */ private static void OnlyRunFiveTimes() throws IOException { // TODO Auto-generated method stub File file = new File("f:\\a.txt"); if(!file.exists()) file.createNewFile(); BufferedReader br = new BufferedReader(new FileReader(file)); Properties pro = new Properties(); pro.load(br); String str = null; int count; if((str=pro.getProperty("count")) == null){ pro.put("count", "1"); }else{ count = Integer.parseInt(str); if(count == 5){ throw new RuntimeException("使用超過五次"); } count++; pro.setProperty("count", count+""); } BufferedWriter bw = new BufferedWriter(new FileWriter(file)); pro.store(bw, "count~~~~"); br.close(); bw.close(); }
}