IO(三)字節流練習

public class ByteStreamDemo {/*int available();                         可以取得輸入文件的大小(字節個數),沒有返回0void close();                            關閉輸入流abstract int read();                    讀取一個字節,并把讀到的字節返回,沒有返回-1int read(byte[] b);                        將內容讀到byte數組,并且返回讀入的字節的個數。,沒有返回-1int read(byte[] b ,int off ,int len);    將內容讀到byte數組,從off開始讀,讀len個結束。,沒有返回-1*//*public void close();                    關閉輸出流。public void flush();                    刷新緩沖區。public void write(byte[] b);            將byte數組寫入數據流write(byte[] b ,int off ,int len);        將指定范圍的數據寫入數據流。public abstract void write(int b);        將一個字節數據寫入數據流*///輸入流沒有找到文件報異常,輸出流沒有文件會自動創建public static void executeByteFile(){File inFile = new File("D:"+File.separator+"intest.txt");File outFile = new File("D:"+File.separator+"outtest.txt");long start = System.currentTimeMillis();FileInputStream in = null;OutputStream out = null;try {in = new FileInputStream(inFile);//true表示在原文件上追加。out = new FileOutputStream(outFile,true);byte[] inb = new byte[1024];int size = 0;while ((size = in.read(inb)) != -1) {
//                System.out.println(new String(inb,0,size,"gbk"));out.write(inb, 0, size);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{CloseUtil.close(out);CloseUtil.close(in);}System.out.println("結束時間:"+(System.currentTimeMillis() - start));}/**操作字節數組的輸入流* @throws IOException */public static void executeByteIn() throws IOException{final String str = "我愛中華!";byte[] bytes = str.getBytes();ByteArrayInputStream byin = new ByteArrayInputStream(bytes);byte[] db = new byte[3];int read = byin.read(db);while ( read !=-1 ) {String s = new String(db,0,read,"UTF-8");System.out.println(s);read = byin.read(db);}byin.close();}/**操作字節數組的輸出流* @throws IOException */public static void executeByteOut() throws IOException{final String str1 = "我愛中華!";final String str2 = "字節數組輸出流!";byte[] bytes1 = str1.getBytes();byte[] bytes2 = str2.getBytes();ByteArrayOutputStream out = new ByteArrayOutputStream();out.write(bytes1);out.write(bytes2);//先把數據都寫進字節數組輸出流中。之后統一輸出
        System.out.println(out.toString());out.close();}/** 管道流*/public static void executePip() throws IOException{PipedByteOut out = new PipedByteOut();PipedByteIn in = new PipedByteIn();out.getOut().connect(in.getIn());Thread inThread = new Thread(in,"thread-piped-in");Thread outThread = new Thread(out,"thread-piped-out");inThread.start();outThread.start();}/** 緩存字節流*/public static void executeBufferStream() throws Exception{File inFile = new File("D:"+File.separator+"intest.txt");File outFile = new File("D:"+File.separator+"outtest.txt");long start = System.currentTimeMillis();BufferedInputStream inbuffer = new BufferedInputStream(new FileInputStream(inFile));BufferedOutputStream outbuffer = new BufferedOutputStream(new FileOutputStream(outFile,true));byte[] inb = new byte[1024];int size = 0;while ((size = inbuffer.read(inb)) != -1) {outbuffer.write(inb, 0, size);}outbuffer.flush();CloseUtil.close(outbuffer);CloseUtil.close(inbuffer);System.out.println("結束時間:"+(System.currentTimeMillis() - start));}/** 對象流可以將對象序列化*/public static class SerializeUtil{public static byte[] serializeObject(Object object){ObjectOutputStream out = null;ByteArrayOutputStream by = new ByteArrayOutputStream();try {out = new ObjectOutputStream(by);out.writeObject(object);return by.toByteArray();} catch (IOException e) {throw new RuntimeException("對象序列化錯誤");}finally{CloseUtil.close(by);CloseUtil.close(out);}}public static <T> T unSerialized(byte[] by){if(by == null || by.length == 0) return null;ByteArrayInputStream byin = null;ObjectInputStream in = null;try {byin = new ByteArrayInputStream(by);in = new ObjectInputStream(byin);return (T) in.readObject();} catch (IOException | ClassNotFoundException e) {throw new RuntimeException("對象反序列化錯誤");}finally{CloseUtil.close(in);CloseUtil.close(byin);}}}public static void main(String[] args) throws Exception{User user = new User();user.setAddres("地址");user.setId(1);user.setName("測試");byte[] bs = SerializeUtil.serializeObject(user);Object object = SerializeUtil.unSerialized(bs);System.out.println(object);}
}class PipedByteIn implements Runnable{private PipedInputStream in = new PipedInputStream();@Overridepublic void run() {try {byte[] by = new byte[1024];int i = in.read(by);while (i != -1) {System.out.println(new String(by,0,i,"UTF-8"));i = in.read(by);}} catch (IOException e) {e.printStackTrace();}finally{CloseUtil.close(in);}}public PipedInputStream getIn(){return in;}
}class PipedByteOut implements Runnable{private PipedOutputStream out = new PipedOutputStream();@Overridepublic void run() {byte[] bytes = "我愛中華!".getBytes();try {out.write(bytes);} catch (IOException e) {e.printStackTrace();}finally{CloseUtil.close(out);}}public PipedOutputStream getOut(){return out;}
}class User implements Serializable{private static final long serialVersionUID = 1L;private Integer id;private String name;//transient代表該字段不被序列化private transient String addres;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddres() {return addres;}public void setAddres(String addres) {this.addres = addres;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", addres=" + addres + "]";}
}
class CloseUtil{public static void close(Closeable closeable){if(closeable != null){try {closeable.close();} catch (IOException e) {e.printStackTrace();}}}
}

轉載于:https://www.cnblogs.com/DivineHost/p/5387807.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373253.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373253.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373253.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于matlab的人臉五官邊緣檢測方法,人臉邊緣檢測方法研究與仿真

人臉表情是人類情感的主載體之一,它含有豐富的人體行為信息。通過臉部表情能夠表達人微妙的情緒反應以及對應的心理狀態[1],人臉表情識別技術隨著人們對表情信息的日益重視而受到關注,現已成為人們研究的熱點。基于幾何特征提取是一個快速、直接、有效的人臉表情識別方法,運用基…

GWT –利弊

我喜歡JavaScript。 隨著jQuery和Mootools的出現&#xff0c;我對JavaScript的熱愛僅增加了很多倍。 只要有選擇&#xff0c;我就可以將上述框架中的任何一個用于我開發的任何Web應用程序。 但是進入服務行業后&#xff0c;我不得不一次次屈服于客戶的壓力&#xff0c;并在他們…

秦九韶算法matlab實驗報告,數值分析上機實驗報告.doc

實驗報告一題目&#xff1a; (緒論) 非線性方程求解及誤差估計摘要&#xff1a;非線性方程的解析解通常很難給出&#xff0c;因此線性方程的數值解法就尤為重要。本實驗采用兩種常見的求解方法二分法、Newton法和改進的Newton法。可以節省計算機的計算時間&#xff0c;還能減小…

Flex 布局教程:語法篇

網頁布局&#xff08;layout&#xff09;是CSS的一個重點應用。 布局的傳統解決方案&#xff0c;基于盒狀模型&#xff0c;依賴 display屬性 position屬性 float屬性。它對于那些特殊布局非常不方便&#xff0c;比如&#xff0c;垂直居中就不容易實現。 2009年&#xff0c;W3…

練習錯誤

form:阻止表單提交的方法一&#xff1a;在form標簽中給出以下代碼&#xff1a; 1 onsubmit "return False" 方法二&#xff1a;設置事件阻止 1 e.preventDefault() js中判斷&#xff1a;只要非數字都應該表示為字符串 1 if(Email.indexOf("") -1){ 2 …

JavaFX 2中的PopupMenu

創建彈出菜單 要在JavaFX中創建Popupmenu&#xff0c;可以使用ContextMenu類。 您向其中添加MenuItems&#xff0c;也可以使用SeparatorMenuItem創建可視分隔符。 在下面的示例中&#xff0c;我選擇子類ContextMenu并將MenuItems添加到其構造函數中。 public class Animatio…

matlab中CH指標聚類評價指標,MATLAB聚類有效性評價指標(外部)

MATLAB聚類有效性評價指標(外部)作者&#xff1a;凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/更多內容&#xff0c;請看標簽&#xff1a;MATLAB、聚類前提&#xff1a;數據的真實標簽已知&#xff01;1. 歸一化互信息(Normalized Mutual information)定義程序functio…

學習進度表

周數 專業學習目標 專業學習時/每分鐘 新增代碼量 知識技能總結 第六周 ps的圖像處理 80 30 看書加以實踐 第七周 數據結構的鏈式結構 100 50 多做習題加以鞏固知識 第八周 網頁設計 80 30 多多練習&#xff0c;學會用代碼設計 第九周 圖片美工 70 30 慢慢學會運用軟…

Axis通過wsdd部署Web Service

axis網上的教程很多&#xff0c;不過搜來搜去&#xff0c;總是只有那么幾篇。仔細看了一下那幾篇文章&#xff0c;都感覺到不是自己想要的&#xff0c;所以自己整理了一篇分享一下。 本文介紹axis應用的一個小例子&#xff0c;沒有麻煩的命令行操作&#xff0c;只需照下面的步驟…

彈簧特性

1.概述 本教程將展示如何通過XML或Java配置在Spring中設置和使用屬性 。 在Spring 3.1之前 &#xff0c;將新的屬性文件添加到Spring并使用屬性值并不像它那樣靈活和健壯。 從Spring 3.1開始 &#xff0c;新的Environment和PropertySource抽象大大簡化了此過程。 2.通過XML名…

php-cgi cpu很高,php-cgi占用cpu資源過高的解決方法

轉的網上的&#xff0c;不過對PHP-CGI菜鳥的人&#xff0c;還是有點幫助的。1. 一些php的擴展與php版本兼容存在問題&#xff0c;實踐證明 eAccelerater與某些php版本兼容存在問題&#xff0c;具體表現時啟動php-cgi進程后&#xff0c;運行10多分鐘&#xff0c;奇慢無比&#x…

《做中學》讀后有感

《做中學》讀后有感 最近讀了婁老師的“做中學”系列文章&#xff0c;有很大感觸&#xff0c;今天想著重談一談我在學習方面收到的啟發。 如何成功get一項技能 如果問到“如何開始get一項技能”&#xff0c;我想我們應該是最有發言權的一代。從小就被爸爸媽媽引導著參加各種課外…

多表之間關聯查詢

內連接 jion on 自連接 本表進行內連接的查詢形式 外鏈接&#xff1a; 左鏈接 寫法&#xff1a;select 字段 from 表1 t left join 表2 s on t.字段1 s.字段1 where 條件 或者 作用&#xff1a;保證左邊的表的數據全部顯示&#xff0c;包括空的 右鏈接 寫法 &#xff1a;sele…

php文件夾0777,PHP代碼mkdir(‘images’,’0777′)創建一個具有411權限的文件夾!為什么?...

我發誓這是昨天的工作.然而,現在下面的代碼破壞文件夾沒有問題,但創建一個具有411權限的新文件夾應該是777.我的代碼昨天這樣做.這樣做的目的是壓縮文件夾,傳遞文件夾,刪除圖像,然后為圖像創建新目錄.有人能告訴我我做錯了什么或我應該做什么&#xff1f;謝謝function delete_d…

調查HashDoS問題

近一個月前&#xff0c;我就如何在不與供應商互動的情況下臨時解決 28C3上出現的HashDoS問題或其他代碼缺陷發表了一些想法。 現在是時候更深入地研究復雜性攻擊并查看來源了。 我完全假設java.util.HashMap和java.util.Hashtable是受此攻擊影響的最常用的Java數據結構&#xf…

Linq 和 EF Contains示例

List<int> unitIDListnew List<int>(); //此處添加int元素 var query DB.ElecConsumers.Where(c > unitIDList.Contains(c.ParentUnitID)); //EF方式 var query1 (from c in DB.ElecConsumers where unitIDList.Contains(c.ParentUnitID ) select c); //Linq方…

date 顯示或設置系統時間和日期

顯示或設置系統時間和日期 date [options] [format] date [options] [new date] date用來顯示系統的時間和日期&#xff0c;超級用戶可以使用date來更改系統時鐘 選項 %H 小時&#xff0c;24小時制&#xff08;00~23&#xff09; %I 小時&#xff0c;12小時制&#xff…

Java 7:WatchService

在Java 7的所有新功能中&#xff0c;更有趣的是WatchService&#xff0c;它增加了監視目錄更改的功能。 WatchService直接映射到本機文件事件通知機制&#xff08;如果有&#xff09;。 如果本機事件通知機制不可用&#xff0c;則默認實現將使用輪詢。 結果&#xff0c;響應性&…

做一件事情的3個關鍵指標:興趣、能力和回報

最近突然有了一點新的感悟&#xff0c;在原有的認識基礎之上。關于找工作&#xff0c;大家說的最多的&#xff0c;根據自己的“興趣”和“能力”。我覺得這是不夠的&#xff0c;還應該加上一個“回報”。興趣&#xff1a;對一件事有沒有愿望去嘗試&#xff0c;側重“好奇心”。…

iOS應用內支付(IAP)詳解

在iOS開發中如果涉及到虛擬物品的購買&#xff0c;就需要使用IAP服務&#xff0c;我們今天來看看如何實現。 在實現代碼之前我們先做一些準備工作&#xff0c;一步步來看。 1、IAP流程 IAP流程分為兩種&#xff0c;一種是直接使用Apple的服務器進行購買和驗證&#xff0c;另一種…