JavaSE各阶段练习题----IO流
| package?com.io; ? import?java.io.FileInputStream; import?java.io.FileOutputStream; import?java.io.IOException; ? /* ?* 1、格式: ? try ( ??? 流對象的創建 ) { ? 流對象的使用 } ? try 后面的小括號中,表示不管 try 塊是正常結束還是異常結束,這個資源都會被自動關閉。 ?*/ public?class?Demo01____7后關閉流的方式 { ? public?static?void?main(String[] args) throws?IOException { ? try( FileInputStream fis?= new?FileInputStream("d:/IO文檔/aaa.txt"); FileOutputStream fos?= new?FileOutputStream("d:/IO文檔/bbb.txt"); ){ ? byte[] bs?= new?byte[1024]; int?len?= 0; while((len?= fis.read(bs)) != -1) { fos.write(bs, 0, len); } ? } } } ? |
| package?com.io; ? import?java.io.FileInputStream; import?java.io.FileNotFoundException; import?java.io.FileOutputStream; import?java.io.IOException; import?java.io.SequenceInputStream; import?java.util.Enumeration; import?java.util.Vector; ? public?class?Demo02_序列流多個文件 { /* ?* 合并多個流 ?* public SequenceInputStream(Enumeration<? extends InputStream> e) ?* 可以看出構造方法給出Enumeration對象作為參數,Enumeration對象會返回一系列InputStream類型的對象, ?* 提供給SequenceInputStream類讀取。 ?*/ public?static?void?main(String[] args) throws?IOException { ? //創建文件輸入流讀取文件 FileInputStream fis1?= new?FileInputStream("d:/IO文檔/aaa.txt"); FileInputStream fis2?= new?FileInputStream("d:/IO文檔/bbb.txt"); FileInputStream fis3?= new?FileInputStream("d:/IO文檔/ccc.txt"); ? //創建Vector對象 Vector<Object> vector?= new?Vector<>(); vector.add(fis1); vector.add(fis2); vector.add(fis3); Enumeration?elements?= vector.elements(); ? SequenceInputStream sis?= new?SequenceInputStream(elements); FileOutputStream fos?= new?FileOutputStream("d:/IO文檔/ddd.txt"); byte[] bs?= new?byte[1024]; int?len?= 0; while((len?= sis.read(bs)) != -1) { fos.write(bs,0,len); } ? fos.close(); sis.close(); fis1.close(); fis2.close(); fis3.close(); } ? } ? |
| package?com.io; ? import?java.io.File; import?java.io.FileInputStream; import?java.io.FileNotFoundException; import?java.io.FileOutputStream; import?java.io.IOException; import?java.io.SequenceInputStream; ? public?class?Demo02_序列流又稱合并流 { /* ?* SequenceInputStream類 ?* ?* 【注】序列流只針對文本文檔即.txt其他文件會有不一樣的錯誤 ?*/ public?static?void?main(String[] args) throws?IOException { /* ?* 兩個文件的合并流 ?*/ try( //創建兩個文件輸入流讀取兩個文件 FileInputStream fis1?= new?FileInputStream("d:/IO文檔/aaa.txt"); FileInputStream fis2?= new?FileInputStream("d:/IO文檔/bbb.txt"); //SequenceInputStream對象用于合并兩個文件輸入流 SequenceInputStream sis?= new?SequenceInputStream(fis1,fis2); FileOutputStream fos?= new?FileOutputStream("d:/IO文檔/ccc.txt"); ){ byte[] bs?= new?byte[1024]; int?len?= 0; while?((len?= sis.read(bs)) != -1) { fos.write(bs, 0, len); //寫入換行 fos.write("\r\n".getBytes()); } } } } ? |
| package?com.io; ? import?java.io.DataInputStream; import?java.io.DataOutputStream; import?java.io.File; import?java.io.FileInputStream; import?java.io.FileNotFoundException; import?java.io.FileOutputStream; ? public?class?Demo03_數據流 { /* ?* DateInputSream ?和 ????DateOutputStream ?* 將基本數據類型進行序列化和反序列化 ?* 而且還提供了readUTF()方法,和writeUTF()方法 ?*/ public?static?void?main(String[] args) throws?Exception { ? FileOutputStream fos?= new?FileOutputStream("d:/IO文檔/ddd.txt"); DataOutputStream dos?= new?DataOutputStream(fos); dos.write(10); ????//寫入數據默認字節 dos.writeChar('中'); dos.writeUTF("加油,你會的到你所想要的一切"); dos.close(); fos.close(); ? FileInputStream fis?= new?FileInputStream("D:/IO文檔/ddd.txt"); DataInputStream dis?= new?DataInputStream(fis); System.out.println(dis.read()); System.out.println(dis.readChar()); System.out.println(dis.readUTF()); dis.close(); fos.close(); ? } ? } ? |
| package?com.io; ? import?java.io.FileInputStream; import?java.io.FileOutputStream; import?java.io.ObjectInputStream; import?java.io.ObjectOutputStream; import?java.io.Serializable; ? public?class?Demo04_對象序列化 { /* ?* 【注】對象必須實現【【【Serializable接口】】】 ?* ?* 序列化:將內存中的對象轉化為二進制數據流的形式輸出,保存到磁盤中(ObjectOutputStream) ?* ?* 反序列化:根據序列化保存的二進制數據文件,恢復到序列化之前的JAVA對象(ObjectInputStream) ?*/ public?static?void?main(String[] args) throws?Exception { ? Student s1?= new?Student(1,"劉蕾助教",18); Student s2?= new?Student(2,"李木大神",22); Student s3?= new?Student(2,"李木大神",22); Student s4?= new?Student(2,"李木大神",22); ? ObjectOutputStream oos?= new?ObjectOutputStream(new?FileOutputStream("stu.txt")); ? oos.writeObject(s1); oos.writeObject(s2); oos.writeObject(s3); oos.writeObject(s4); ? oos.close(); ? ObjectInputStream ois?= new?ObjectInputStream(new?FileInputStream("stu.txt")); ? Student o1?= (Student) ois.readObject(); Student o2?= (Student) ois.readObject(); Student o3?= (Student) ois.readObject(); Student o4?= (Student) ois.readObject(); ? System.out.println(o1); System.out.println(o2); System.out.println(o3); System.out.println(o4); } } ? ? class?Student?implements?Serializable{ private?int?id; private?String name; private?int?age; public?Student() { } public?Student(int?id, String name, int?age) { this.id?= id; this.name?= name; this.age?= age; } public?int?getId() { return?id; } public?void?setId(int?id) { this.id?= id; } public?String getName() { return?name; } public?void?setName(String name) { this.name?= name; } public?int?getAge() { return?age; } public?void?setAge(int?age) { this.age?= age; } @Override public?String toString() { return?"Student [id="?+ id?+ ", name="?+ name?+ ", age="?+ age?+ "]"; } } ? ? |
| package?com.io; ? import?java.io.FileInputStream; import?java.io.FileOutputStream; import?java.io.ObjectInputStream; import?java.io.ObjectOutputStream; import?java.io.Serializable; ? public?class?Demo05_序列化中遇到的問題 { /* ?* 當連續兩次序列化同一對象引用時,并不會有兩個對象被序列化。 ?* 第2次只是輸出一個序列號編號。即使當第一序列化完成后,修改了對象的值,再次進行序列化, ?* 被序列化對象的值也不會發生變化。 ?* ?* 并且如果你在實體類中刪除或者添加新的內容,則出現InvalidClassException錯誤 ?*/ public?static?void?main(String[] args) throws?Exception { ? Student1 s1?= new?Student1(1,"劉蕾助教",18); Student1 s2?= new?Student1(2,"李木大神",22); Student1 s3?= new?Student1(3,"李鑫大神"); ? ObjectOutputStream oos?= new?ObjectOutputStream(new?FileOutputStream("stu2.txt")); ? oos.writeObject(s1); s1.setAge(80); oos.writeObject(s1); // 和第1次寫的是同一個對象,第2次序列化時并不會有兩個對象被序列化。 //第2次只是輸出一個序列號編號 oos.writeObject(s2); oos.writeObject(s3); oos.close(); ? ObjectInputStream ois?= new?ObjectInputStream(new?FileInputStream("stu2.txt")); ? Student1 o1?= (Student1) ois.readObject(); Student1 o2?= (Student1) ois.readObject(); Student1 o3?= (Student1) ois.readObject(); Student1 o4?= (Student1) ois.readObject(); ? System.out.println(o1); System.out.println(o2); System.out.println(o3); System.out.println(o4); ? /* ?* Student [id=1, name=劉蕾助教, age=18] Student [id=1, name=劉蕾助教, age=18] Student [id=2, name=李木大神, age=22] ?*/ ? } } ? class?Student1?implements?Serializable{ private?int?id; private?String name; private?int?age; public?Student1() { } public?Student1(int?id, String name) { this.id?= id; this.name?= name; } public?Student1(int?id, String name, int?age) { this.id?= id; this.name?= name; this.age?= age; } public?int?getId() { return?id; } public?void?setId(int?id) { this.id?= id; } public?String getName() { return?name; } public?void?setName(String name) { this.name?= name; } public?int?getAge() { return?age; } public?void?setAge(int?age) { this.age?= age; } @Override public?String toString() { return?"Student [id="?+ id?+ ", name="?+ name?+ ", age="?+ age?+ "]"; } } |
| package?com.io; ? import?java.io.FileInputStream; import?java.io.FileOutputStream; import?java.io.ObjectInputStream; import?java.io.ObjectOutputStream; import?java.io.Serializable; ? public?class?Demo06_序列化中的版本號問題 { ? public?static?void?main(String[] args) throws?Exception { ? Student2 s1?= new?Student2(1,"劉蕾助教",18); Student2 s2?= new?Student2(2,"李木大神",22); Student2 s3?= new?Student2(2,"李木大神"); ? ObjectOutputStream oos?= new?ObjectOutputStream(new?FileOutputStream("stu1.txt")); ? oos.writeObject(s1); s1.setAge(80); oos.writeObject(s1); oos.writeObject(s2); oos.writeObject(s3); oos.close(); ? ObjectInputStream ois?= new?ObjectInputStream(new?FileInputStream("stu1.txt")); ? Student2 o1?= (Student2) ois.readObject(); Student2 o2?= (Student2) ois.readObject(); Student2 o3?= (Student2) ois.readObject(); Student2 o4?= (Student2) ois.readObject(); ? System.out.println(o1); System.out.println(o2); System.out.println(o3); System.out.println(o4); ? /* ?* Student [id=1, name=劉蕾助教, age=18] Student [id=1, name=劉蕾助教, age=18] Student [id=2, name=李木大神, age=22] Student [id=2, name=李木大神, age=0] ?*/ ? } ? } class?Student2 implements?Serializable{ /** ?* 下面這個即為版本號 ?*/ private?static?final?long?serialVersionUID?= 1L; private?int?id; private?String name; private?int?age; public?Student2() { } public?Student2(int?id, String name) { this.id?= id; this.name?= name; } public?Student2(int?id, String name, int?age) { this.id?= id; this.name?= name; this.age?= age; } public?int?getId() { return?id; } public?void?setId(int?id) { this.id?= id; } public?String getName() { return?name; } public?void?setName(String name) { this.name?= name; } public?int?getAge() { return?age; } public?void?setAge(int?age) { this.age?= age; } @Override public?String toString() { return?"Student [id="?+ id?+ ", name="?+ name?+ ", age="?+ age?+ "]"; } } ? |
| package?com.io; ? import?java.io.FileNotFoundException; import?java.io.RandomAccessFile; ? /* ?* RandomAccessFile支持對文件進行非順序的、隨機的訪問。 ? 不是一個流對象,不在IO體系中,但是比流對象更加的強大;既可以讀,也可以寫;可以隨機訪問。 ? 構造方法: ? RandomAccessFile(String path, String mode) ? path:文件的路徑 ? mode:操作文件的模式, “r”是只讀,”rw”是讀寫 ?"rws"是每次都更新到潛在的存儲設備中 s指同步(synchronous) ? ?????? ???"rwd"是將文件內容更新到存儲設備中去 ? ??它實現了DataInput 和DataOutput 接口,并提供了三個額外方法: ???????int?skipBytes(int) ??向前移動文件指針指定的字節數 ???????void seek(long) ????文件指針定位,參數為偏移量 ???????long getFilePointer()返回當前文件指針的位置(相對文件首的偏移量) ???????[補充] void setLength(long newLength) 設置文件長度 ?*/ public?class?Demo07_RandomAccessFile { public?static?void?main(String[] args) throws?Exception { ? /*RandomAccessFile raf?= new RandomAccessFile("data.txt","r"); raf.skipBytes(5); ?//向后移動5個字節,再開始讀數據 ? int?i = raf.read(); System.out.println(i); ? raf.seek(5); ?// 直接把指針定位的某個位置 ? String str2 = raf.readLine(); System.out.println(str2); ? raf.close();*/ ? ?? RandomAccessFile raf?= new?RandomAccessFile("data.txt","rw"); ? ? raf.writeInt(200); raf.seek(10); raf.writeInt(300); ? ? int?x?= raf.readInt(); System.out.println(x); ? raf.seek(10); ? int?y?= raf.readInt(); System.out.println(y); ? raf.close(); ? } } ? |
| package?com.io; ? import?java.io.File; import?java.io.FileInputStream; import?java.io.FileNotFoundException; import?java.io.FileOutputStream; import?java.io.IOException; import?java.io.InputStream; import?java.util.zip.ZipEntry; import?java.util.zip.ZipOutputStream; ? public?class?Demo08_壓縮流 { ZipOutputStream zos?= null; ? public?Demo08_壓縮流 (){ try?{ zos= new?ZipOutputStream(new?FileOutputStream("F:\\0715大數據.zip")); } catch?(FileNotFoundException e) { e.printStackTrace(); } } ? public?static?void?main(String[] args) { File f?= new?File("F:\\0715大數據"); Demo08_壓縮流 ?zzz?= new?Demo08_壓縮流 (); zzz.zip(f); zzz.close(); } // 傳入要壓縮的文件夾 public??void?zip(File file){ File[] fs?= file.listFiles(); for(File f:fs){ if(f.isFile()){ zipFile(f); }else{ zip(f); ?// 是文件夾,遞歸調用 } } } //壓縮文件 public?void?zipFile(File f){ try?{ // System.out.println(f.getPath().substring(3)); zos.putNextEntry(new?ZipEntry(f.getPath().substring(3))); // 如果傳入ZipEntry的文件名帶有目錄,在壓縮時,壓縮包中就會生成目錄 // 一個ZipEntry是一個要被壓縮的項 InputStream is?= new?FileInputStream(f); byte[] b?= new?byte[1024]; int?len?= 0; while((len=is.read(b))!=-1){ zos.write(b,0, len); } zos.closeEntry(); is.close(); ? } catch?(IOException e) { e.printStackTrace(); } } public?void?close(){ try?{ zos.close(); } catch?(IOException e) { e.printStackTrace(); } } ? } ? |
?
總結
以上是生活随笔為你收集整理的JavaSE各阶段练习题----IO流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaSE各阶段练习题----文件和I
- 下一篇: JavaSE各阶段练习题----多线程