IO流05--畢向東JAVA基礎教程視頻學習筆記,05--畢向東

來源:互聯網
上載者:User

IO流05--畢向東JAVA基礎教程視頻學習筆記,05--畢向東

Day20

10 建立java檔案清單
11 Properties簡述
12 Properties存取
13 Properties存取設定檔
14 Properties練習
15 PrintWriter
16 合并流
17 切割檔案

 

10 建立java檔案清單

練習:
將一個指定目錄下的java檔案的絕對路徑,儲存到一個文字檔中,
建立一個java檔案清單的檔案。

思路:
1.對指定的目錄進行遞迴
2.擷取遞迴過程所有的java檔案的路徑
3.將這些路徑儲存到集合中
4.將集合中的資料存放區到一個檔案中。

1 import java.io.*; 2 import java.util.*; 3 public class JavaFileList 4 { 5 public static void main(String[] args) throws IOException 6 { 7 File dir=new File("d:\\abc"); 8 List<File> list=new ArrayList<File>(); 9 fileToList(dir,list);10 File file=new File(dir,"javafile.txt");11 writeToFile(list,file.toString());12 13 14 }15 public static void fileToList(File dir,List<File> list) 16 {17 File[] files=dir.listFiles();18 19 for(File file:files)20 {21 if(file.isDirectory())22 fileToList(file,list);23 else24 {25 if(file.getName().endsWith(".java"))26 list.add(file);27 }28 }29 }30 public static void writeToFile(List<File> list,String javaListFile)throws IOException 31 {32 BufferedWriter bufw=null;33 try34 {35 bufw=new BufferedWriter(new FileWriter(javaListFile));36 for(File f:list)37 {38 String path=f.getAbsolutePath();39 bufw.write(path);40 bufw.newLine();41 bufw.flush();42 }43 44 }45 catch (IOException e)46 {47 throw e;48 }49 finally50 {51 try52 {53 if(bufw!=null)54 bufw.close();55 }56 catch (IOException ie)57 {58 throw ie;59 }60 }61 }62 }View Code

 

11 Properties簡述

Properties是hashtable的子類。
也就是說它具備map集合的特點,而且它裡面儲存的索引值對都是字串,
是集合和IO技術相結合的集合容器。
該對象的特點,可以用於索引值對形式的設定檔

 

設定檔:就是在使用者登入電腦時,或是使用者在使用軟體時,軟體系統為使用者所要載入所需環境的設定和檔案的集合。

它包括所有使用者專用的配置設定,如程式項目、螢幕顏色、網路連接、印表機串連、滑鼠設定及視窗的大小和位置等。


12 Properties存取

String getProperty(String key):用指定的鍵在此屬性類表中搜尋屬性。

Object setProperty(String key,String value):調用Hashtable的方法put.

Set<String> stringPropertyNames():返回此屬性類表中的鍵集。

 

1 import java.util.*; 2 public class PropertiesDemo 3 { 4 public static void main(String[] args) 5 { 6 setAndGet(); 7 8 } 9 public static void setAndGet()10 {11 Properties prop=new Properties();12 13 prop.setProperty("Tina","22");14 prop.setProperty("Jack","21");15 prop.setProperty("Marry","20");16 17 String value=prop.getProperty("Jack");18 System.out.println(value);19 20 Set<String> names=prop.stringPropertyNames();21 for(String s:names)22 {23 System.out.println(s+":"+prop.getProperty(s));24 }25 }26 }View Code

 

13 Properties存取設定檔

如何將流中的資料存放區到集合中
想要將info.txt中索引值資料存到集合中進行操作
1.用一個流和info.txt檔案相關聯。
2.讀取一行資料,將該行資料用“=”進行分割,並且存入Properties數組中。

Properties類在載入資料時,需要資料有固定的格式,通常是鍵=值。

1 import java.util.*; 2 import java.io.*; 3 public class PropertiesDemo_2 4 { 5 public static void main(String[] args)throws IOException 6 { 7 method_1(); 8 //loadDemo(); 9 }10 public static void method_1()throws IOException11 {12 BufferedReader bufr=new BufferedReader(new FileReader("info.txt"));13 String line=null;14 Properties prop=new Properties();15 while((line=bufr.readLine())!=null)16 {17 String[] arr=line.split("=");18 19 prop.setProperty(arr[0],arr[1]);20 }21 System.out.println(prop);22 bufr.close();23 }24 //load和store方法的使用25 public static void loadDemo()throws IOException26 {27 Properties prop=new Properties();28 FileInputStream fis=new FileInputStream("info.txt");29 30 //將流中的資料載入進集合31 prop.load(fis);32 //改變集合內容33 prop.setProperty("Tina","16");34 FileOutputStream fos=new FileOutputStream("info.txt");35 //改變檔案內容,使用store方法36 prop.store(fos,"hahaha~~~");37 38 System.out.println(prop);39 fis.close();40 fos.close();41 42 }43 44 }View Code

 

14 Properties練習

用於記錄應用程式運行次數,
如果使用次數已到,那麼給出註冊提示。

很容易想到的是:計數器
可是該計數器定義在程式中,隨著程式的運行而在記憶體中存在,並自行自增,
可是隨著該應用程式的退出,該計數器也在記憶體中消失了。

下一次啟動該程式,又會重新從0開始計數,
這樣不是我們想要的。
程式即使結束,該計數器的值也應該存在。
下次程式啟動會在載入該計數器的並加1後重新儲存起來。

所以要建立一個設定檔,用於記錄該軟體的使用次數。

該設定檔使用索引值對的形式,這樣便於閱讀和操作資料。
索引值對資料是map集合,
資料是以檔案形式儲存,使用io技術。
那麼map+io-->properties

設定檔可以實現應用程式資料的共用。

具體實現代碼如下:

1 import java.io.*; 2 import java.util.*; 3 public class RunCount 4 { 5 public static void main(String[] args) throws IOException 6 { 7 Properties prop=new Properties(); 8 //把檔案封裝成對象 9 File file=new File("count.ini");10 //如果檔案不存在,建立檔案11 if(!file.exists())12 file.createNewFile();13 FileInputStream fis=new FileInputStream(file);14 //從輸入資料流中讀取屬性列表15 prop.load(fis);16 17 //定義變數,記錄運行次數18 int count=0;19 String value=prop.getProperty("time");20 21 if(value!=null)22 {23 count=Integer.parseInt(value);24 if(count>=5)25 {26 System.out.println("您好,使用次數已到,請註冊後再使用!");27 return;28 }29 }30 count++;31 //改變Properties表中的索引值32 prop.setProperty("time",count+"");33 34 FileOutputStream fos=new FileOutputStream(file);35 //將Properties表中的鍵和元素對寫入輸出資料流36 prop.store(fos,"");37 38 //關閉輸入輸出資料流39 fos.close();40 fis.close();41 42 }43 }View Code

運行情況:

運行第六次時,控制台的顯示:

您好,使用次數已到,請註冊後再使用!

count.ini檔案的內容:

#
#Wed Jan 13 16:33:13 CST 2016
time=5

 

15 PrintWriter

列印流:
該流提供了許多列印方法,可以將各種資料類型的資料都原樣列印。

位元組列印流:
PrintStream
建構函式可以接受的參數類型:
1.File對象 File
2.字串路徑 String
3.位元組輸出資料流 OutputStream

字元列印流:
PrintWriter
建構函式可以接受的參數類型:
1.File對象 File
2.字串路徑 String
3.位元組輸出資料流 OutputStream
4.字元輸出資料流 Writer

1 import java.io.*; 2 public class PrintDemo 3 { 4 public static void main(String[] args) throws IOException 5 { 6 BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in)); 7 //自動重新整理 8 //PrintWriter out=new PrintWriter(System.out,true); 9 PrintWriter out=new PrintWriter(new FileWriter("g.txt"),true);10 11 String line=null;12 while((line=bufr.readLine())!=null)13 {14 if("over".equals(line))15 break;16 //帶換行的列印17 out.println(line.toUpperCase());18 //out.flush();19 20 }21 out.close();22 bufr.close();23 24 }25 }View Code

 

16 合并流

SequenceInputStream 表示其他輸入資料流的邏輯串聯。它從輸入資料流的有序集合開始,並從第一個輸入資料流開始讀取,

直到到達檔案末尾,接著從第二個輸入資料流讀取,依次類推,直到到達包含的最後一個輸入資料流的檔案末尾為止。

可以用來合并檔案。

1 import java.io.*; 2 import java.util.*; 3 public class SequenceInputStreamDemo 4 { 5 public static void main(String[] args) throws IOException 6 { 7 Vector<FileInputStream> v=new Vector<FileInputStream>(); 8 9 v.add(new FileInputStream("d:\\abc\\1.txt"));10 v.add(new FileInputStream("d:\\abc\\2.txt"));11 v.add(new FileInputStream("d:\\abc\\3.txt"));12 13 Enumeration<FileInputStream> en=v.elements();14 15 SequenceInputStream sis=new SequenceInputStream(en);16 FileOutputStream fos=new FileOutputStream("d:\\abc\\4.txt");17 18 byte[] buf=new byte[1024];19 20 int len=0;21 while((len=sis.read(buf))!=-1)22 {23 fos.write(buf,0,len);24 }25 fos.close();26 sis.close();27 28 29 }30 }View Code

 

17 切割檔案

檔案可以合并,同樣也可以切割。當網站的一次上傳大小有一定限制時,需要上傳較大檔案時,就需要用到檔案的切割。

1 import java.io.*; 2 //切割檔案 3 public class SplitFile 4 { 5 public static void main(String[] args)throws IOException 6 { 7 splitFile(); 8 9 }10 public static void splitFile()throws IOException 11 {12 FileInputStream fis=new FileInputStream("d:\\abc\\bird.jpg");13 FileOutputStream fos=null;14 15 16 byte[] buf=new byte[1024];17 int len=0;18 int count =1;19 while((len=fis.read(buf))!=-1)20 {21 fos=new FileOutputStream("d:\\abc\\"+(count++)+".part");22 fos.write(buf,0,len);23 fos.close();24 25 }26 fis.close();27 28 }29 }View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.