1. 程式人生 > >學習筆記之JavaSE(40)--IO流2

學習筆記之JavaSE(40)--IO流2

今天學習的內容是位元組流和位元組流緩衝區

位元組流與字元流的基本操作相同,注意位元組流的輸出沒有用到緩衝區,下例使用FileOutputStream類FileInputStream類實現對文字檔案的讀寫(位元組流不能直接處理字元內容,只能直接處理位元組內容):

public class Test78 {

	public static void main(String[] args) {

		test_write();
		test_read_1();
		test_read_2();
	}

	private static void test_read_2() {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("test.txt");
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = fis.read(buf)) != -1) {
				System.out.print(new String(buf, 0, len));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
		}
	}

	private static void test_read_1() {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("test.txt");
			int ch = 0;
			while ((ch = fis.read()) != -1) {
				System.out.print((char) ch);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
		}
	}

	private static void test_write() {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("test.txt");
			fos.write("asdasd".getBytes());// 位元組流操作的是位元組
			// 注意與字元流不同,位元組流並沒有用到緩衝區,不用flush()
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
		}
	}
}

與字元流一樣,位元組流也有對應的緩衝區--BufferedOutputStream類和BufferedInputStream類,下面分別使用基本方法和緩衝區實現圖片和MP3的複製
public class Test79 {

	public static void main(String[] args) {

		copy_picture_1();
		copy_picture_2();
		copy_mp3_1();
		copy_mp3_2();
	}

	private static void copy_mp3_2() {

		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream("蕭敬騰 - Beautiful Love.mp3"));
			out = new BufferedOutputStream(new FileOutputStream("蕭敬騰 - Beautiful Love_copy_2.mp3"));
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = in.read(buf)) != -1) {
				out.write(buf, 0, len);
				out.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
		}
	}

	private static void copy_mp3_1() {

		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("蕭敬騰 - Beautiful Love.mp3");
			fos = new FileOutputStream("蕭敬騰 - Beautiful Love_copy_1.mp3");
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = fis.read(buf)) != -1) {
				fos.write(buf, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
		}
	}

	private static void copy_picture_2() {

		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream("IO.jpg"));
			out = new BufferedOutputStream(new FileOutputStream("IO_copy_2.jpg"));
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = in.read(buf)) != -1) {
				out.write(buf, 0, len);
				out.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
		}
	}

	private static void copy_picture_1() {

		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("IO.jpg");
			fos = new FileOutputStream("IO_copy_1.jpg");
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = fis.read(buf)) != -1) {
				fos.write(buf, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("關閉資源失敗");
				}
			}
		}
	}
}


相關推薦

學習筆記JavaSE40--IO2

今天學習的內容是位元組流和位元組流緩衝區 位元組流與字元流的基本操作相同,注意位元組流的輸出沒有用到緩衝區,下例使用FileOutputStream類和FileInputStream類實現對文字檔案的讀寫(位元組流不能直接處理字元內容,只能直接處理位元組內容): pub

學習筆記JavaSE47--IO9

public class Test91 { public static void main(String[] args) throws ClassNotFoundException { // 物件序列化 ObjectOutputStream oos = null; try { oos

JavaSE 學習筆記封裝

延遲加載 分類 static str super 想要 oid 懶漢式 可靠性 封 裝(面向對象特征之一):是指隱藏對象的屬性和實現細節,僅對外提供公共訪問方式。 好處:將變化隔離;便於使用;提高重用性;安全性。 封裝原則:將不需要對外提供的內容都隱藏起來,把屬性都隱藏,提

JavaSE 學習筆記繼承

內容 訪問 類繼承 mil 抽象方法 ted 內部 -- 中一 繼 承(面向對象特征之一) 好處: 1:提高了代碼的復用性。 2:讓類與類之間產生了關系,提供了另一個特征多態的前提。 父類的由來:其實是由多個類不斷向上抽取共性內容而來的。 java中對於繼承,java只

學習筆記iptables

1.防火牆的基礎知識         首先需要認識到什麼是防火牆,防火牆是通過一些有順序的規則。給從網路中進入到主機應用層之間的通道上設定很多道攔截的口,每個口會有一堆規則去匹配。匹配上,如果是匹配結果是通過就放行,如果是匹配結果是拒絕,就不

Java學習筆記FreeTTS語音

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

AAC學習筆記Dagger

本文為《Android Architecture Components學習筆記》的一部分 文件程式碼為Kotlin,但是系統生成的程式碼仍然為Java 本人水平有限,如有不當之處請不吝賜教 Dagger初接觸 Dagger並不是AAC的一部分,但是在專案中

JavaWeb學習筆記XML1

文章目錄 XML 表單提交方式 XML的介紹 XML的應用 XML的語法 XML的dtd約束 schema約束 相關知識: https://blog.csd

TypeScript學習筆記 介面Interface

在java中,介面是用來定義一些規範,使用這些介面,就必須實現介面中的方法,而且介面中的屬性必須是常量。 javascript中是沒有介面的概念的。所以TypeScript在編譯成 JavaScrip

機器學習筆記SVMSVR演算法

學過SVM後,看了那麼多別人的文章,是時候自己總結一波了。權當寫的筆記供自己日後再回顧吧。 PS:結合自己在工作過程中(我這裡用SVR做股票預測)用到的知識來寫的,不會很全面,若有些知識這裡沒提及讀者

java學習筆記webservice--WSDL文件及用myeclipse測試webservice

 >>接上篇 一、WSDL 定義:web services description language,用來描述web服務的xml格式的資訊。 標籤的解釋 1. <types>:定義了服務的namespace和關鍵資訊的型別(方法的引數型別和返回值的

glib學習筆記——GLib核心應用支援:The Main Event Loop

原文地址 描述 The main event loop manages all the available sources of events for GLib and GTK+ applications. These events can come from any n

leetcode刷題40組合總和2

題目描述 給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中只能使用一次。 說明: 所有數字(包括目標數)都是正整數。 解集不能包

JavaSE:IO

一、概序    Java IO:通過資料流、序列化和檔案系統提供系統輸入和輸出,即Java 輸入輸出系統。    流:資料流,從中讀到資料,往裡面寫入資料。流是資料流向的媒介。二、型別數據流的方向不同可

設計模式C++學習筆記十三Decorator裝飾模式

com img c++ 進行 done 設計 out set 筆記 裝飾模式,動態地給一個對象添加一些額外的職責。就增加功能來說,Decorator模式相比生成子類更為靈活。 13.1.解釋 main(),老爸 ISchoolReport,成績單接口 CFourthGrad

python學習筆記socket第七天

.cn 七天 就是 模塊 AR 操作 alt 分享圖片 python學習 參考文檔: 1、金角大王博客:http://www.cnblogs.com/alex3714/articles/5227251.html

JavaWeb學習筆記XML2

文章目錄 xml的解析(jaxp) dom方式解析xml sax方式解析xml dom4j解析器 相關知識: https://blog.csdn.net/mokexfdgh/articl

LTE學習筆記CSIChannel State Information

基本概念 CSI 是通道狀態資訊, Channel Status Information, 它是一個衡量通道好壞的指標。  有三個引數:CQI,PMI和RI。根據網路狀態和配置,這三個引數通過不同形式的組合成為CSI上報,不一定三個引數全都上上報。 FAQ CSI是一個很複

Socket學習筆記常用基本函式

函式:u_long htonl(u_long hostlong)u_short htons(u_short hostshort)u_long ntohl(u_long netlong)u_short ntohs(u_short netshort)這上面四個函式類似,功能相似,都用來轉換資料格式。用

nsq源碼閱讀筆記nsqd——nsqd的配置解析和初始化

con views pos 直接 rgba 函數調用 程序 spa 重命名 配置解析nsqd的主函數位於apps/nsqd.go中的main函數首先main函數調用nsqFlagset和Parse進行命令行參數集初始化, 然後判斷version參數是否存在,若存在,則打印版