IO流大總結

轉載請註明出處,謝謝!html

<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p> <p align="center"><font color="#FF3030" size="5">寫在前面</font></p> <p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>java

  • 1.概念
    • IO流用來處理設備之間的數據傳輸
    • Java對數據的操做是經過流的方式
    • Java用於操做流的類都在IO包中
    • 流按流向分爲兩種:輸入流,輸出流
    • 流按操做類型分爲兩種:
      • 字節流 : 字節流能夠操做任何數據,由於在計算機中任何數據都是以字節的形式存儲的
      • 字符流 : 字符流只能操做純字符數據,比較方便
  • 2.IO流經常使用父類
    • 字節流的抽象父類:
      • InputStream
      • OutputStream
    • 字符流的抽象父類:
      • Reader
      • Writer
  • 3.IO程序書寫
    • 使用前,導入IO包中的類
    • 使用時,進行IO異常處理
    • 使用後,釋放資源

<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>windows

<p align="center"><font color="#FF3030" size="5">目&emsp;錄</font></p>設計模式

<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p> <br/>數組

<p align="center"><font face="仿宋" color="#004D88" size="4">1.read()一次讀取一個字節</font></p>dom

<p align="center"><font face="仿宋" color="#004D88" size="4">2.read()方法返回值爲何是int</font></p>ide

<p align="center"><font face="仿宋" color="#004D88" size="4">3.定義小數組實現緩衝</font></p>優化

<p align="center"><font face="仿宋" color="#004D88" size="4">4.實現了緩衝區的BufferedInputStream和BufferOutputStream</font></p>ui

<p align="center"><font face="仿宋" color="#004D88" size="4">5.flush方法和close方法</font></p>this

<p align="center"><font face="仿宋" color="#004D88" size="4">6. 字節流讀寫中文</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">7.流的標準處理異常代碼1.6版本及其之前</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">8.流的標準處理異常代碼1.7版本以後</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">9.拷貝文件</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">10.錄入數據拷貝到文件</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">11.字符流 FileReader</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">12.字符流 FileWriter</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">13.字符流的拷貝</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">14.什麼狀況下使用字符流</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">15.字符流是否能夠拷貝非純文本的文件</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">16.自定義字符數組的拷貝</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">17.帶緩衝的字符流</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">18.readLine()和newLine()方法</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">19.LineNumberReader</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">20.裝飾設計模式</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">21.使用指定的碼錶讀寫字符</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">22.序列流</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">23.序列流整合多個</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">24.內存輸出流</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">25.定義一個文件輸入流,調用read(byte[] b)方法,將a.txt文件中的內容打印出來(byte數組大小限制爲5)</font>

<p align="center"><font face="仿宋" color="#004D88" size="4">26.對象操做流ObjecOutputStream</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">27.對象操做流ObjectInputStream</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">28.對象操做流優化</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">29.id號</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">30.打印流的概述和特色</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">31.標準輸入輸出流概述和輸出語句</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">32.修改標準輸入輸出流拷貝圖片</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">33.兩種方式實現鍵盤錄入</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">34.隨機訪問流概述和讀寫數據</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">35.數據輸入輸出流</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">36.Properties的概述和做爲Map集合的使用</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">37.獲取Properties中的每個鍵</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">38.Properties的load()和store()功能</font></p>

<p align="center"><font face="仿宋" color="#004D88" size="4">39.ZIP文檔</font></p>

<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>


<br/>

<br/>

<br/>

<span id = "1"></span>

<font color="#FF6600" size="4">read()一次讀取一個字節</font>

FileInputStream fis = new FileInputStream("烏合之衆.txt");	
	//建立一個文件輸入流對象,並關聯烏合之衆.txt
	int b;													
	//定義變量,記錄每次讀到的字節
	while((b = fis.read()) != -1) {							
	//將每次讀到的字節賦值給b並判斷是不是-1
		System.out.println(b);								
		//打印每個字節
	}
	
	fis.close();											
//關閉流釋放資源

<br/>

<span id = "2"></span>

<font color="#FF6600" size="4">read()方法返回值爲何是int</font>

  • read()方法讀取的是一個字節,爲何返回是int,而不是byte
  • 由於字節輸入流能夠操做任意類型的文件,好比圖片音頻等,這些都是以二進制形式的存儲的,若是每次讀取都返回byte,有可能在讀到中間的時候遇到11111111,那麼這11111111byte類型的-1,程序是遇到-1就會中止,後面的數據就讀不到了。因此在讀取的時候用int類型接收,會在其前面補上24個0湊足4個字節,那麼byte類型的-1就變成int類型的255了這樣能夠保證整個數據讀完,而結束標記的-1就是int類型。

<br/> <span id = "3"></span>

<font color="#FF6600" size="4">定義小數組實現緩衝</font>

  • 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();

<br/> <span id = "4"></span>

<font color="#FF6600" size="4">實現了緩衝區的BufferedInputStream和BufferOutputStream</font>

  • 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操做的是兩個數組.

<br/> <span id = "5"></span>

<font color="#FF6600" size="4">flush方法和close方法</font>

  • flush()方法
    • 用來刷新緩衝區的,刷新後能夠再次寫出
  • close()方法
    • 用來關閉流釋放資源的的,若是是帶緩衝區的流對象的close()方法,不但會關閉流,還會再關閉流以前刷新緩衝區,關閉後不能再寫出

<br/> <span id = "6"></span>

<font color="#FF6600" size="4">字節流讀寫中文</font>

  • 字節流讀取中文的問題
    • 字節流在讀中文的時候有可能會讀到半個中文,形成亂碼
  • 字節流寫出中文的問題
    • 字節流直接操做的字節,因此寫出中文必須將字符串轉換成字節數組
    • 寫出回車換行 write("\r\n".getBytes());

<br/> <span id = "7"></span> ## ~~<font color="#FF6600" size="4">**流的標準處理異常代碼1.6版本及其之前**</font>~~

  • 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();
			}
		}

<br/> <span id = "8"></span>

<font color="#FF6600" size="4">流的標準處理異常代碼1.7版本以後</font>

  • 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方法將流關掉.

<br/> <span id = "9"></span>

<font color="#FF6600" size="4">拷貝文件</font>

  • 在控制檯錄入文件的路徑,將文件拷貝到當前項目下
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();

<br/> <span id = "10"></span> ## <font color="#FF6600" size="4">**錄入數據拷貝到文件**</font>

  • 將鍵盤錄入的數據拷貝到當前項目下的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();

<br/> <span id = "11"></span> ## <font color="#FF6600" size="4">**字符流 FileReader**</font>

  • 字符流是什麼

    • 字符流是能夠直接讀寫字符的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();
 //關流

<br/> <span id = "12"></span> ## <font color="#FF6600" size="4">**字符流 FileWriter**</font>

  • FileWriter類的write()方法能夠自動把字符轉爲字節寫出
FileWriter fw = new FileWriter("to.txt");
fw.write("write");
fw.close();

<br/> <span id = "13"></span> ## <font color="#FF6600" size="4">**字符流的拷貝**</font>

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();

<br/> <span id = "14"></span>

<font color="#FF6600" size="4">什麼狀況下使用字符流</font>

  • 字符流也能夠拷貝文本文件, 但不推薦使用。由於讀取時會把字節轉爲字符, 寫出時還要把字符轉回字節。
  • 程序須要讀取一段文本, 或者須要寫出一段文本的時候可使用字符流。讀取的時候是按照字符的大小讀取的,不會出現讀取半個中文,形成亂碼的狀況。寫出的時候能夠直接將字符串寫出,不用轉換爲字節數組。

<span id = "15"></span>

<font color="#FF6600" size="4">字符流是否能夠拷貝非純文本的文件</font>

  • 不能夠拷貝非純文本的文件
    • 由於在讀的時候會將字節轉換爲字符,在轉換過程當中,可能找不到對應的字符,就會用"?"代替,寫出的時候會將"?"字符轉換成字節寫出去。如此這般,寫出以後的文件就錯亂了。

<br/> <span id = "16"></span>

<font color="#FF6600" size="4">自定義字符數組的拷貝</font>

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();

<br/> ## <span id = "17"></span> <font color="#FF6600" size="4">**帶緩衝的字符流**</font>

  • BufferedReaderread()方法讀取字符時會一次讀取若干字符到緩衝區, 而後逐個返回給程序, 減小讀取次數, 以期提升效率。
  • BufferedWriterwrite()方法寫出字符時會先寫到緩衝區, 緩衝區寫滿時纔會寫到文件, 減小寫入次數, 以期提升效率。
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();

<br/> <span id = "18"></span>

<font color="#FF6600" size="4">readLine()和newLine()方法</font>

  • BufferedReaderreadLine()方法能夠讀取一行字符(不包含換行符號)
  • BufferedWriternewLine()能夠輸出一個跨平臺的換行符號"\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();

<br/> <span id = "19"></span>

<font color="#FF6600" size="4">LineNumberReader</font>

  • LineNumberReaderBufferedReader的子類, 具備相同的功能, 而且能夠統計行號
    • 調用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();

<br/> <span id = "20"></span>

<font color="#FF6600" size="4">裝飾設計模式</font>

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("......");
        }
        
    }

<br/> <span id = "21"></span>

<font color="#FF6600" size="4">使用指定的碼錶讀寫字符</font>

  • 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();

<br/> <span id = "22"></span>

<font color="#FF6600" size="4">序列流</font>

  • 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();

<br/> <span id = "23"></span>

<font color="#FF6600" size="4">序列流整合多個</font>

  • 整合多個: 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();

<br/> <span id = "24"></span>

<font color="#FF6600" size="4">內存輸出流</font>

  • 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();

<br/> <span id = "25"></span>

<font color="#FF6600" size="4">定義一個文件輸入流,調用read(byte[] b)方法,將a.txt文件中的內容打印出來(byte數組大小限制爲5)</font>

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();

<br/> <span id = "26"></span>

<font color="#FF6600" size="4">對象操做流ObjecOutputStream</font>

  • 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();
	}

}

<br/> <span id = "27"></span>

<font color="#FF6600" size="4">對象操做流ObjectInputStream</font>

  • 讀取: 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();
		}			
}

<br/> <span id = "28"></span>

<font color="#FF6600" size="4">對象操做流優化</font>

  • 將對象存儲在集合中寫出
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();

<br/> <span id = "29"></span>

<font color="#FF6600" size="4">id號</font>

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

<br/> <span id = "30"></span>

<font color="#FF6600" size="4">打印流的概述和特色</font>

  • 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();

<br/> <span id = "31"></span>

<font color="#FF6600" size="4">標準輸入輸出流概述和輸出語句</font>

  • 1.什麼是標準輸入輸出流
    • System.inInputStream, 標準輸入流, 默承認以從鍵盤輸入讀取字節數據
    • System.outPrintStream, 標準輸出流, 默承認以向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();

<br/> <span id = "32"></span>

<font color="#FF6600" size="4">修改標準輸入輸出流拷貝圖片</font>

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();

<br/> <span id = "33"></span>

<font color="#FF6600" size="4">兩種方式實現鍵盤錄入</font>

  • A:BufferedReaderreadLine方法。
    • BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  • B:Scanner

<br/> <span id = "34"></span>

<font color="#FF6600" size="4">隨機訪問流概述和讀寫數據</font>

  • A:隨機訪問流概述

    • RandomAccessFile類不屬於流,是Object類的子類。但它融合了InputStreamOutputStream的功能。
    • 支持對隨機訪問文件的讀取和寫入。
  • B:read(),write(),seek()

<br/> <span id = "35"></span>

<font color="#FF6600" size="4">數據輸入輸出流</font>

  • 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();

<br/> <span id = "36"></span>

<font color="#FF6600" size="4">Properties的概述和做爲Map集合的使用</font>

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

<br/> <span id = "37"></span>

<font color="#FF6600" size="4">獲取Properties中的每個鍵</font>

  • 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);
}

<br/> <span id = "38"></span>

<font color="#FF6600" size="4">Properties的load()和store()功能</font>

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}

<br/>

<span id = "39"></span>

<font color="#FF6600" size="4">ZIP文檔</font>

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

<span id = "zz"></span>

——@guoyangde http://www.cnblogs.com/LittleTreasureBox/p/8904016.html

相關文章
相關標籤/搜索