IO流的分類、序列化、單例

流的分類:

(一) 按字節分

1.字節流

  • IpurStream,OutputStream

2.字符流

  • Reader(BufferReader),Writer(BufferWriter)

(二)按方向分

1.輸入流

  • InputStream,Reader

2.輸出流

  • OutputStream.Writer

(三)按功能分

1.包裝流(轉換流)

  • InputStreamReader,OutputStreamWriter

2.節點流

  • 除了包裝流都是節點流

重點:

1.高性能的包裝流

  • InputStreamReader/Writer
  • BufferedReader/Writer
  • URL:全球統一資源定位符(即我們常見的網址,可以理解爲鏈接)
		
public class Text {
public static void main(String[] args) throws Exception {
		URL url=new URL("http://www.baidu.com/");
		InputStream is = url.openStream();// 字節流
		InputStreamReader  isr=newInputStreamReader(is,"UTF-8");
		BufferedReader br=new BufferedReader(isr);
		String line=null;
		while((line=br.readLine())!=null){
			System.out.println(line);
		}
	}

2.字節流拷貝(萬能拷貝)

public class Text {
public static void main(String[] args) throws IOException{
	copy();// 拷貝
}

private static void copy() throws IOException {
		// 只適應於純文本。
	long currentTimeMillis = System.currentTimeMillis();
	BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("a.mp4"), "UTF-8"));
	BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a1.mp4"),"UTF-8"));
		String line=null;
		while((line=br.readLine())!=null){
			bw.write(line);
		}
		br.close();
		bw.close();
		System.out.println(System.currentTimeMillis()-currentTimeMillis);
	}

}

2.Serialiable序列化(寫到文件中,可以持久化)

  • 序列化:將一個對象寫到文件中的過程叫序列化。對象可序列化
  • 反序列化:將一個對象從文件中讀出的過程叫做反序列化。
  • 反序列化有問題,單例模式(懶漢、餓漢式單利)只要經過反序列化就會被破壞-------->解決方案:重寫 readResolve()方法
  • SerialiableUID的用途:標識序列化版本(保證文件和類中一致)

3. 單例模式

  • 懶漢式
  • 餓漢式
  • 枚舉
  • 正經單例
  • 靜態內部類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章