Day2010 創建java文件列表11 Properties簡述12 Properties存取13 Properties存取配置文件14 Properties練習15 PrintWriter16 合併流17 切割文件10 創建java文件列表練習:將一個指定目錄下的java文件的絕對路徑,存儲到一...
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 else
24 {
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 try
34 {
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 finally
50 {
51 try
52 {
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 IOException
11 {
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 IOException
26 {
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