Java之IO流進階篇:記憶體流,列印流,物件流
阿新 • • 發佈:2018-12-28
Java中的IO流,即為輸入輸出流。所謂輸入輸出流,都是相對於程式而言,程式就是這個參照物。一張圖看懂輸入輸出流:
輸入流抽象基類:InputStream,Reader
輸出流抽象基類:OutputStream,Writer
輸入輸出流子類眾多,詳情見下圖:
1.記憶體流
用來操作記憶體
ByteArrayInputStream 記憶體到程式 不需要關閉 不使用記憶體資源,記憶體不夠建議不用
ByteArrayOutputStream 程式到記憶體 不需要關閉 不使用記憶體資源,記憶體不夠建議不用
記憶體輸入流和記憶體輸出流:
package com.test; import java.io.ByteArrayInputStream; import java.io.IOException; public class ByteArrayInputStreamDemo { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //1.建立位元組陣列(要讀取的資料) byte[] data= {10,15,50,33,12,22};//2.建立流 ByteArrayInputStream bais=new ByteArrayInputStream(data); //3.讀取 // int d; // while((d=bais.read())!=-1) { // System.out.println(d); // } // bais.close();//實際操作中無需關閉流 byte[] buf=new byte[1024]; int len; while((len=bais.read(buf))!=-1) {for (int i = 0; i < len; i++) { System.out.println(buf[i]); } } } }
package com.test; import java.io.ByteArrayOutputStream; public class ByteArrayOutputStreamDemo { public static void main(String[] args) { // TODO Auto-generated method stub //1.建立位元組陣列輸出流物件 ByteArrayOutputStream baos=new ByteArrayOutputStream(); //2.寫入資料 baos.write(12); baos.write(20); baos.write(18); baos.write(32); //3.獲取輸出流中的位元組資料 byte[] data=baos.toByteArray(); for (byte b : data) { System.out.println(b); } } }
使用記憶體流讀取圖片:
package com.test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class ReadImg { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //1.建立檔案位元組輸入流 FileInputStream fis=new FileInputStream("E:\\xx.png"); ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte[] buf=new byte[1024]; //2.讀取資料 int len; while((len=fis.read(buf))!=-1) { baos.write(buf,0,len); } //3.獲取圖片資料 byte[] imgbyte=baos.toByteArray(); System.out.println(imgbyte.length); //4.建立檔案輸出流 FileOutputStream fos=new FileOutputStream("E:\\xx1.png"); ByteArrayInputStream bais=new ByteArrayInputStream(imgbyte); //5.讀取陣列 byte[] buf1=new byte[1024]; int len1; while((len1=bais.read(buf1))!=-1) { fos.write(buf1,0,len1); } fis.close(); fos.close(); bais.close(); baos.close(); } }
2.列印流
PrintSream:操作位元組,自動重新整理,可設定字符集
PrintWriter :不能操作位元組,內部有緩衝區
列印位元組流例項:
package com.test; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; public class PrintStreamDemo { //列印流(位元組) public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //PrintStream ps=new PrintStream("E:\\aaa.txt"); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("E:\\xx.txt")); PrintStream ps=new PrintStream(bos,true); ps.print("123456"); ps.print(true); ps.println("abcdef"); ps.printf("%d", 200); ps.printf("%.2f", 3.1415926); ps.printf("%s", "我愛生活"); ps.printf("%x", 256); } }
列印字元流例項:
package com.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class PrintWriterDemo { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub PrintWriter pw=new PrintWriter(new FileOutputStream("E:\\bbb.txt")); pw.println("123"); pw.close(); } }
3.物件序列化與反序列化(物件流)
本例舉一個學生類Student.class序列化和反序列化的過程。
建立的學生類:
package com.test; import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID=123L;//序列化與反序列化的唯一標記 private int StuNo;//序列化和訪問許可權沒有關係 String name; //transient int age;//transient 瞬時的,不能序列化瞬時的屬性 //static String address="北京";//靜態變數不能被序列化 public Student() { super(); // TODO Auto-generated constructor stub } public Student(int stuNo, String name) { super(); StuNo = stuNo; this.name = name; } public int getStuNo() { return StuNo; } public void setStuNo(int stuNo) { StuNo = stuNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
使用物件類序列化和反序列化學生類:
package com.test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { //序列化過程: //1.建立序列化物件 Student stu=new Student(1101,"小明"); //2.建立檔案輸出流 FileOutputStream fos=new FileOutputStream("E:\\receive.bin"); //3.建立物件流接入輸出流 ObjectOutputStream oos=new ObjectOutputStream(fos); //4.物件流輸出序列化物件儲存在檔案中 oos.writeObject(stu); oos.close(); //反序列化過程: //1.建立物件流 FileInputStream fis=new FileInputStream("E:\\receive.bin") ; ObjectInputStream ois=new ObjectInputStream(fis); //2.反序列化 Student stu1=(Student)ois.readObject(); ois.close(); System.out.println("學號:"+stu1.getStuNo()); System.out.println("姓名:"+stu1.getName()); } }
控制檯輸出資訊(表明反序列化成功):
4.屬性集