天天看點

IO流大總結

轉載請注明出處,謝謝!

- - - - - - - - - - - - - - -

寫在前面

- - - - - - - - - - - - - - -
  • 1.概念
    • IO流用來處理裝置之間的資料傳輸
    • Java對資料的操作是通過流的方式
    • Java用于操作流的類都在IO包中
    • 流按流向分為兩種:輸入流,輸出流
    • 流按操作類型分為兩種:
      • 位元組流 : 位元組流可以操作任何資料,因為在計算機中任何資料都是以位元組的形式存儲的
      • 字元流 : 字元流隻能操作純字元資料,比較友善
  • 2.IO流常用父類
    • 位元組流的抽象父類:
      • InputStream

      • OutputStream

    • 字元流的抽象父類:
      • Reader

      • Writer

  • 3.IO程式書寫
    • 使用前,導入IO包中的類
    • 使用時,進行IO異常處理
    • 使用後,釋放資源
- - - - - - - - - - - - - - -

目 錄

- - - - - - - - - - - - - - -

1.read()一次讀取一個位元組

2.read()方法傳回值為什麼是int

3.定義小數組實作緩沖

4.實作了緩沖區的BufferedInputStream和BufferOutputStream

5.flush方法和close方法

6. 位元組流讀寫中文

7.流的标準處理異常代碼1.6版本及其以前

8.流的标準處理異常代碼1.7版本之後

9.拷貝檔案

10.錄入資料拷貝到檔案

11.字元流 FileReader

12.字元流 FileWriter

13.字元流的拷貝

14.什麼情況下使用字元流

15.字元流是否可以拷貝非純文字的檔案

16.自定義字元數組的拷貝

17.帶緩沖的字元流

18.readLine()和newLine()方法

19.LineNumberReader

20.裝飾設計模式

21.使用指定的碼表讀寫字元

22.序列流

23.序列流整合多個

24.記憶體輸出流

25.定義一個檔案輸入流,調用read(byte[] b)方法,将a.txt檔案中的内容列印出來(byte數組大小限制為5)

26.對象操作流ObjecOutputStream

27.對象操作流ObjectInputStream

28.對象操作流優化

29.id号

30.列印流的概述和特點

31.标準輸入輸出流概述和輸出語句

32.修改标準輸入輸出流拷貝圖檔

33.兩種方式實作鍵盤錄入

34.随機通路流概述和讀寫資料

35.資料輸入輸出流

36.Properties的概述和作為Map集合的使用

37.擷取Properties中的每一個鍵

38.Properties的load()和store()功能

39.ZIP文檔

- - - - - - - - - - - - - - -

read()一次讀取一個位元組

FileInputStream fis = new FileInputStream("烏合之衆.txt");  
    //建立一個檔案輸入流對象,并關聯烏合之衆.txt
    int b;                                                  
    //定義變量,記錄每次讀到的位元組
    while((b = fis.read()) != -1) {                         
    //将每次讀到的位元組指派給b并判斷是否是-1
        System.out.println(b);                              
        //列印每一個位元組
    }
    
    fis.close();                                            
//關閉流釋放資源                

read()方法傳回值為什麼是int

  • read()

    方法讀取的是一個位元組,為什麼傳回是

    int

    ,而不是

    byte

  • 因為位元組輸入流可以操作任意類型的檔案,比如圖檔音頻等,這些都是以二進制形式的存儲的,如果每次讀取都傳回

    byte

    ,有可能在讀到中間的時候遇到

    11111111

    ,那麼這

    11111111

    byte

    類型的

    -1

    ,程式是遇到

    -1就會停止

    ,後面的資料就讀不到了。是以在讀取的時候用

    int

    類型接收,會在其前面補上24個0湊足4個位元組,那麼

    byte

    類型的

    -1

    就變成

    int

    類型的

    255

    了這樣可以保證整個資料讀完,而結束标記的

    -1

    就是

    int

    類型。

定義小數組實作緩沖

  • write(byte[] b)

  • write(byte[] b, int off, int len)

    寫出有效的位元組個數
  • 定義小數組的标準格式
FileInputStream fis = new FileInputStream("李志 - 梵高先生.flac");
        FileOutputStream fos = new FileOutputStream("梵高先生.flac");
        int len;
        byte arr[] = new byte[8*1024];
        //自定義位元組數組
        while((len=fis.read(arr))!=-1){
            fos.write(arr, 0, len);
            //寫出位元組數組寫出有效個位元組個數
        }
        fis.close();
        fos.close();                

實作了緩沖區的BufferedInputStream和BufferOutputStream

  • BufferedInputStream

    • BufferedInputStream

      内置了一個緩沖區(數組)
    • BufferedInputStream

      中讀取一個位元組時,

      BufferedInputStream

      會一次性從檔案中讀取

      8192

      個, 存在緩沖區中, 然後傳回給程式一個字元。
    • 程式再次讀取時, 就不用找檔案了, 直接從緩沖區中擷取。
    • 直到緩沖區中所有的都被使用過, 才重新從檔案中讀取

      8192

  • BufferedOutputStream

    • BufferedOutputStream

      也内置了一個緩沖區(數組)
    • 程式向流中寫出位元組時, 不會直接寫到檔案, 先寫到緩沖區中,
    • 直到緩沖區寫滿,

      BufferedOutputStream

      才會把緩沖區中的資料一次性寫到檔案裡。
  • 組合流過濾器實作拷貝
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("李志 - 梵高先生.flac"));
        //建立緩沖區對FileInputStream對象的裝飾
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2004-梵高先生.flac"));
        //建立緩沖區對FileOutputStream對象的裝飾
int b;
while((b = bis.read()) != -1) {     
    bos.write(b);
}
        
bis.close();//隻關裝飾後的對象即可
bos.close();                
  • 小數組的讀寫和帶

    Buffered

    的讀取哪個更快?
    • 定義小數組如果是

      8192

      個位元組大小和

      Buffered

      比較的話,定義小數組會略勝一籌,因為讀和寫操作的是同一個數組,而

      Buffered

      操作的是

      兩個數組

      .

flush方法和close方法

  • flush()

    方法
    • 用來重新整理緩沖區的,重新整理後可以再次寫出
  • close()

    方法
    • 用來關閉流釋放資源的的,如果是帶緩沖區的流對象的

      close()

      方法,不但會關閉流,還會再關閉流之前重新整理緩沖區,關閉後不能再寫出

位元組流讀寫中文

  • 位元組流讀取中文的問題
    • 位元組流在讀中文的時候有可能會讀到半個中文,造成亂碼
  • 位元組流寫出中文的問題
    • 位元組流直接操作的位元組,是以寫出中文必須将字元串轉換成位元組數組
    • 寫出回車換行 write("\r\n".getBytes());

流的标準處理異常代碼1.6版本及其以前

  • try finally嵌套
FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("from.txt");
            fos = new FileOutputStream("to.txt");
            int b;
            while((b = fis.read()) != -1) {
                fos.write(b);
            }
        } finally {
            try {
                if(fis != null)
                    fis.close();
            }finally {
                if(fos != null)
                    fos.close();
            }
        }                

流的标準處理異常代碼1.7版本之後

  • try close
try(
    FileInputStream fis = new FileInputStream("from.txt");
    FileOutputStream fos = new FileOutputStream("to.txt");
){
    int b;
    
    while((b = fis.read()) != -1) {
        fos.write(b);
    }
    
}                
  • 在try()中建立的流對象必須實作了AutoCloseable這個接口,如果實作了,在try後面的

    {. . .}

    執行後就會自動調用流對象的close方法将流關掉.

拷貝檔案

  • 在控制台錄入檔案的路徑,将檔案拷貝到目前項目下
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個檔案路徑");
String line = sc.nextLine();                //将鍵盤錄入的檔案路徑存儲在line中
File file = new File(line);                 //封裝成File對象
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getName());

int len;
byte[] arr = new byte[8192];                //定義緩沖區
while((len = fis.read(arr)) != -1) {
    fos.write(arr,0,len);
}

fis.close();
fos.close();                

錄入資料拷貝到檔案

  • 将鍵盤錄入的資料拷貝到目前項目下的text.txt檔案中,鍵盤錄入資料當遇到quit時就退出
Scanner sc = new Scanner(System.in);
FileOutputStream fos = new FileOutputStream("text.txt");
System.out.println("請輸入:");
while(true) {
    String line = sc.nextLine();
    if("quit".equals(line))
        break;
    fos.write(line.getBytes());
    fos.write("\r\n".getBytes());
}

fos.close();                

字元流 FileReader

  • 字元流是什麼
    • 字元流是可以直接讀寫字元的IO流
    • 字元流讀取

      字元

      , 就要先讀取到

      位元組

      資料, 然後轉為

      字元

      . 如果要寫出字元, 需要把字元轉為位元組再寫出.
  • FileReader
    • FileReader類的read()方法可以按照字元大小讀取
FileReader fr = new FileReader("from.txt"); 
 //建立輸入流對象,關聯from.txt 
 int ch; 
 while((ch = fr.read()) != -1) { 
 //将讀到的字元指派給ch 
 System.out.println((char)ch); 
 //将讀到的字元強轉後列印 
 }

 fr.close();
 //關流 
           

字元流 FileWriter

  • FileWriter類的write()方法可以自動把

    字元

    轉為

    位元組

    寫出
FileWriter fw = new FileWriter("to.txt");
fw.write("write");
fw.close();                

字元流的拷貝

FileReader fr = new FileReader("from.txt");
FileWriter fw = new FileWriter("to.txt");

int ch;
while((ch = fr.read()) != -1) {
    fw.write(ch);
}

fr.close();
fw.close();                

什麼情況下使用字元流

  • 字元流也可以拷貝文本檔案, 但不推薦使用。因為讀取時會把位元組轉為字元, 寫出時還要把字元轉回位元組。
  • 程式需要讀取一段文本, 或者需要寫出一段文本的時候可以使用字元流。讀取的時候是按照字元的大小讀取的,不會出現讀取半個中文,造成亂碼的情況。寫出的時候可以直接将字元串寫出,不用轉換為位元組數組。

字元流是否可以拷貝非純文字的檔案

  • 不可以拷貝非純文字的檔案
    • 因為在讀的時候會将位元組轉換為字元,在轉換過程中,可能找不到對應的字元,就會用

      "?"

      代替,寫出的時候會将

      "?"

      字元轉換成位元組寫出去。如此這般,寫出之後的檔案就錯亂了。

自定義字元數組的拷貝

FileReader fr = new FileReader("form.txt");          
//建立字元輸入流,關聯aaa.txt
FileWriter fw = new FileWriter("to.txt");          
//建立字元輸出流,關聯bbb.txt

int len;
char[] arr = new char[1024*8];                      
//建立字元數組
while((len = fr.read(arr)) != -1) {                 
//将資料讀到字元數組中
    fw.write(arr, 0, len);                          
    //從字元數組将資料寫到檔案上
}

fr.close();
//關流釋放資源
fw.close();                 

帶緩沖的字元流

  • BufferedReader

    read()

    方法讀取字元時會一次讀取若幹字元到緩沖區, 然後逐個傳回給程式, 減少讀取次數, 以期提高效率。
  • BufferedWriter

    write()

    方法寫出字元時會先寫到緩沖區, 緩沖區寫滿時才會寫到檔案, 減少寫入次數, 以期提高效率。
BufferedReader br = new BufferedReader(new FileReader("form.txt")); 
//建立字元輸入流對象,關聯aaa.txt 
BufferedWriter bw = new BufferedWriter(new FileWriter("to.txt")); 
//建立字元輸出流對象,關聯bbb.txt

int ch;             
while((ch = br.read()) != -1) {
//read一次,會先将緩沖區讀滿,從緩沖去中一個一個的返給臨時變量ch
    bw.write(ch);
    //write一次,是将資料裝到字元數組,裝滿後再一起寫出去
}
    
br.close(); 
//關流
bw.close();                  

readLine()和newLine()方法

  • BufferedReader

    readLine()

    方法可以讀取一行字元(不包含換行符号)
  • BufferedWriter

    newLine()

    可以輸出一個跨平台的換行符号"\r\n"
BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt"));
String line; 
while((line = br.readLine()) != null) {
   bw.write(line); 
   //bw.write(""); //隻支援windows系統 
   bw.newLine(); //跨平台的 
}

br.close();
bw.close();                 

LineNumberReader

  • LineNumberReader

    BufferedReader

    的子類, 具有相同的功能, 并且可以統計行号
    • 調用

      getLineNumber()

      方法可以擷取目前行号
    • 調用

      setLineNumber()

      方法可以設定目前行号
LineNumberReader lnr = new LineNumberReader(new FileReader("form.txt")); 
String line; 
lnr.setLineNumber(100); //設定行号 
while((line = lnr.readLine()) != null) {
    System.out.println(lnr.getLineNumber() + ":" + line);//擷取行号 
}

lnr.close();                 

裝飾設計模式

interface Coder {
    public void code(); 
 }                
class Persion implements Coder {
    
        @Override
        public void code() {
            System.out.println("It's none of my business during the daytime");
            System.out.println("Write java at night");
        }
        
    }
           
class XPersion implements Coder {
        private Persion s;                      
        //被包裝的類的引用
        public XPersion (Persion s) {       
        //構造方法将被包裝的對象作為參數傳入
            this.s = s;
        }
        @Override
        public void code() {
        //對其原有功能進行更新
            s.code();
            System.out.println("Get cervical spondum buff");
            System.out.println("......");
            System.out.println("sudden death");
            System.out.println("......");
        }
        
    }                 

使用指定的碼表讀寫字元

  • FileReader是使用預設碼表讀取檔案, 如果需要使用指定碼表讀取, 那麼可以使用

    InputStreamReader(位元組流,編碼表)

  • FileWriter是使用預設碼表寫出檔案, 如果需要使用指定碼表寫出, 那麼可以使用

    OutputStreamWriter(位元組流,編碼表)

BufferedReader br =  new BufferedReader(new InputStreamReader(new FileInputStream("UTF-8.txt"), "UTF-8"));
BufferedWriter bw =  new BufferedWriter(new OutputStreamWriter(new FileOutputStream("GBK.txt"), "GBK")); 
int ch; 
while((ch = br.read()) != -1) { 
    bw.write(ch); 
}

br.close();
bw.close();                

序列流

  • 1.什麼是序列流
    • 序列流可以把多個位元組輸入流整合成一個, 從序列流中讀取資料時, 将從被整合的第一個流開始讀, 讀完一個之後繼續讀第二個, 以此類推.
  • 2.使用方式
    • 整合兩個:

      SequenceInputStream(InputStream, InputStream)

FileInputStream fis1 = new FileInputStream("a.txt");            
//建立輸入流對象,關聯a.txt
FileInputStream fis2 = new FileInputStream("b.txt");            
//建立輸入流對象,關聯b.txt
SequenceInputStream sis = new SequenceInputStream(fis1, fis2);  
//将兩個流整合成一個流
FileOutputStream fos = new FileOutputStream("c.txt");           
//建立輸出流對象,關聯c.txt
        
int b;
while((b = sis.read()) != -1) {                                 
//用整合後的輸入流
    fos.write(b);                                               
    //寫到指定檔案上
}
        
sis.close();
fos.close();                 

序列流整合多個

  • 整合多個:

    SequenceInputStream(Enumeration)

FileInputStream fis1 = new FileInputStream("a.txt");    
//建立輸入流對象,關聯a.txt
FileInputStream fis2 = new FileInputStream("b.txt");    
//建立輸入流對象,關聯b.txt
FileInputStream fis3 = new FileInputStream("c.txt");    
//建立輸入流對象,關聯c.txt
Vector<InputStream> v = new Vector<>();                 
//建立vector集合對象
v.add(fis1);                                            
//将流對象添加
v.add(fis2);
v.add(fis3);
Enumeration<InputStream> en = v.elements();             
//擷取枚舉引用
SequenceInputStream sis = new SequenceInputStream(en);  
//en傳遞給SequenceInputStream的構造方法
FileOutputStream fos = new FileOutputStream("d.txt");
int b;
while((b = sis.read()) != -1) {
    fos.write(b);
}
    
sis.close();
fos.close();
           

記憶體輸出流

  • 1.什麼是記憶體輸出流
    • 該輸出流可以向記憶體中寫資料, 把記憶體當作一個緩沖區, 寫出之後可以一次性擷取所有資料
  • 2.使用方式
    • 建立對象:

      new ByteArrayOutputStream()

    • 寫出資料:

      write(int), write(byte[])

    • 擷取資料:

      toByteArray()

FileInputStream fis = new FileInputStream("a.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int b;
while((b = fis.read()) != -1) {
    baos.write(b);
}
            
//byte[] newArr = baos.toByteArray();               
//将記憶體緩沖區中所有的位元組存儲在newArr中
//System.out.println(new String(newArr));
System.out.println(baos);
fis.close();                

定義一個檔案輸入流,調用read(byte[] b)方法,将a.txt檔案中的内容列印出來(byte數組大小限制為5)

FileInputStream fis = new FileInputStream("a.txt");             
//建立位元組輸入流,關聯a.txt
ByteArrayOutputStream baos = new ByteArrayOutputStream();       
//建立記憶體輸出流
byte[] arr = new byte[5];                                       
//建立位元組數組,大小為5
int len;
while((len = fis.read(arr)) != -1) {                            
//将檔案上的資料讀到位元組數組中
    baos.write(arr, 0, len);                                    
    //将位元組數組的資料寫到記憶體緩沖區中
}
System.out.println(baos);                                       
//将記憶體緩沖區的内容轉換為字元串列印
fis.close();                

對象操作流ObjecOutputStream

  • 1.什麼是對象操作流
    • 該流可以将一個對象寫出, 或者讀取一個對象到程式中. 也就是序列化和反序列化的操作.
  • 2.使用方式
    • 寫出:

      new ObjectOutputStream(OutputStream)

      writeObject()

public class ObjectOutputStream {

    /**
     * @param args
     * @throws IOException 
     * 将對象寫出,序列化
     */
     
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));//建立對象輸出流
        oos.writeObject(p1);
        oos.writeObject(p2);
        oos.close();
    }

}                

對象操作流ObjectInputStream

  • 讀取:

    new ObjectInputStream(InputStream)

    ,

    readObject()

public class ObjectInputStream {

        /**
         * @param args
         * @throws IOException 
         * @throws ClassNotFoundException 
         * @throws FileNotFoundException 
         * 讀取對象,反序列化
         */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
        Person p1 = (Person) ois.readObject();
        Person p2 = (Person) ois.readObject();
        System.out.println(p1);
        System.out.println(p2);
        ois.close();
        }           
}
           

對象操作流優化

  • 将對象存儲在集合中寫出
Person p1 = new Person("Tom", 20);
Person p2 = new Person("Jerry", 22);
Person p3 = new Person("Jack", 10);
Person p4 = new Person("Herry", 20);

ArrayList<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt"));
oos.writeObject(list);                                  
//寫出集合對象

oos.close();                
  • 讀取到的是一個集合對象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt"));
ArrayList<Person> list = (ArrayList<Person>)ois.readObject();   
//泛型在運作期會被擦除,索引運作期相當于沒有泛型
//想去掉黃色可以加注解@SuppressWarnings("unchecked")
for (Person person : list) {
    System.out.println(person);
}
        
ois.close();                

id号

  • 要寫出的對象必須實作Serializable接口才能被序列化
  • 不用必須加id号

列印流的概述和特點

  • 1.什麼是列印流
    • 該流可以很友善的将對象的

      toString()

      結果輸出, 并且自動加上換行, 而且可以使用自動刷出的模式
    • System.out

      就是一個

      PrintStream

      , 其預設向控制台輸出資訊
PrintStream ps = System.out;
ps.println(97);                 
//底層用的是Integer.toString(x),将x轉換為數字字元串列印
ps.println("a string");
ps.println(new Person("Tom", 20));
Person p = null;
ps.println(p);                  //如果是null,就傳回null,如果不是null,就調用對象的toString()                
  • 2.使用方式
    • 列印:

      print()

      ,

      println()

    • 自動刷出:

      PrintWriter(OutputStream out, boolean autoFlush, String encoding)

PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true);
//如果為 true,則 println、printf 或 format 方法将重新整理輸出緩沖區
pw.write(97);
pw.print("Hello");
pw.println("你好");

pw.close();                

标準輸入輸出流概述和輸出語句

  • 1.什麼是标準輸入輸出流
    • System.in

      InputStream

      , 标準輸入流, 預設可以從鍵盤輸入讀取位元組資料
    • System.out

      PrintStream

      , 标準輸出流, 預設可以向Console中輸出字元和位元組資料
  • 2.修改标準輸入輸出流
    • 修改輸入流:

      System.setIn(InputStream)

    • 修改輸出流:

      System.setOut(PrintStream)

System.setIn(new FileInputStream("a.txt"));             
//修改标準輸入流
System.setOut(new PrintStream("b.txt"));                
//修改标準輸出流

InputStream in = System.in;                             
//擷取标準輸入流
PrintStream ps = System.out;                            
//擷取标準輸出流
int b;
while((b = in.read()) != -1) {                          
//從a.txt上讀取位元組
    ps.write(b);                                        
    //将資料寫到b.txt上
}

in.close();
ps.close();                

修改标準輸入輸出流拷貝圖檔

System.setIn(new FileInputStream("png.png"));       
//改變标準輸入流
System.setOut(new PrintStream("copy.png"));         
//改變标準輸出流

InputStream is = System.in;                         
//擷取标準輸入流
PrintStream ps = System.out;                        
//擷取标準輸出流

int len;
byte[] arr = new byte[1024 * 8];

while((len = is.read(arr)) != -1) {
    ps.write(arr, 0, len);
}

is.close();
ps.close();                

兩種方式實作鍵盤錄入

  • A:

    BufferedReader

    readLine

    方法。
    • BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  • B:

    Scanner

随機通路流概述和讀寫資料

  • A:随機通路流概述
    • RandomAccessFile

      類不屬于流,是

      Object

      類的子類。但它融合了

      InputStream

      OutputStream

      的功能。
    • 支援對随機通路檔案的讀取和寫入。
  • B:

    read()

    ,

    write()

    ,

    seek()

資料輸入輸出流

  • 1.什麼是資料輸入輸出流
    • DataInputStream

      ,

      DataOutputStream

      可以按照基本資料類型大小讀寫資料
    • 例如按Long大小寫出一個數字, 寫出時該資料占8位元組. 讀取的時候也可以按照Long類型讀取, 一次讀取8個位元組.
  • 2.使用方式
    • DataOutputStream(OutputStream)

      ,

      writeInt()

      ,

      writeLong()

    • DataInputStream(InputStream)

      ,

      readInt()

      ,

      readLong()

DataOutputStream dos = new DataOutputStream(new FileOutputStream("b.txt"));
dos.writeInt(998);
dos.writeInt(1998);
dos.writeInt(2998);

dos.close();                
DataInputStream dis = new DataInputStream(new FileInputStream("b.txt"));
int x = dis.readInt();
int y = dis.readInt();
int z = dis.readInt();
System.out.println(x);
System.out.println(y);
System.out.println(z);
dis.close();                

Properties的概述和作為Map集合的使用

  • A:Properties的概述
    • Properties 類表示了一個持久的屬性集。
    • Properties 可儲存在流中或從流中加載。
    • 屬性清單中每個鍵及其對應值都是一個字元串。
Properties prop = new Properties();
prop.put("abc", 123);
System.out.println(prop);                

擷取Properties中的每一個鍵

  • A:Properties的特殊功能
    • public Object setProperty(String key,String value)

    • public String getProperty(String key)

    • public Enumeration<String> stringPropertyNames()

Properties prop = new Properties();
prop.setProperty("name", "Tom");
prop.setProperty("tel", "18000000000");

//System.out.println(prop);
Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
while(en.hasMoreElements()) {
    String key = en.nextElement();              //擷取Properties中的每一個鍵
    String value = prop.getProperty(key);       //根據鍵擷取值
    System.out.println(key + "="+ value);
}                

Properties的load()和store()功能

Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));        
//将檔案上的鍵值對讀取到集合中
prop.setProperty("tel", "18912345678");
prop.store(new FileOutputStream("config.properties"), null);
//第二個參數是對清單參數的描述,可以給值,也可以給null
System.out.println(prop);

Output:
{tel=18912345678}
                

ZIP文檔

private static void zipdemo1() throws IOException {
    try (
        ZipInputStream zin = new ZipInputStream(new FileInputStream("code.zip"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();//記憶體輸入流
        ) {
            
        ZipEntry entry;
        int len;
        byte arr[] = new byte[1024];
            
        while ((entry = zin.getNextEntry()) != null) {
                
            String name = entry.getName();//壓縮項目的檔案名
            System.out.println("檔案"+name+"\n解壓後大小:"+entry.getSize());
            while ((len = zin.read(arr)) != -1) {//讀取資料内容
                baos.write(arr, 0, len);//内容寫入記憶體輸入流
            }
                
            System.out.println("緩沖區大小:"+baos.size());
            System.out.println(baos);
            baos.reset();//将此 byte 數組輸出流的 count 字段重置為零,進而丢棄輸出流中目前已累積的所有輸出
            zin.closeEntry();
                
        }
    }
}

Output:

檔案code.txt
解壓後大小:29
緩沖區大小:29
AW9C2-JN9T2-H5CBV-24QT7-G4YB8
檔案code1.txt
解壓後大小:39
緩沖區大小:39
激活碼:W9WW6-JN9W2-M8CTX-24QR7-M4HB8                
——@guoyangde http://www.cnblogs.com/LittleTreasureBox/p/8904016.html

轉載于:https://www.cnblogs.com/LittleTreasureBox/p/8892112.html