過濾流FilterInputStream實現類之BufferedInputStream,物件流ObjectOutputStream 實現資料持久化
阿新 • • 發佈:2018-11-17
使用過濾流(高階流)BufferedInputStream、BufferedInputStream優化檔案拷貝功能
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author cuijiao
*
*/
public class Filestream {
public static void main(String[] args) throws IOException {
File src = new File("D:\\data\\test.jpg");
File target = new File("E:\\copy.jpg");
copyFileByBuffer(src, target);
}
/**
* 拷貝檔案:將src複製到target
*
* @param src
* @param target
* @throws IOException
*/
private static void copyFileByBuffer(File src, File target) throws IOException {
if (!src.exists()) {
return;
}
if (!target.exists()) {
target.createNewFile();
}
FileInputStream fis = null;
FileOutputStream fos = null ;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
// 優先使用高階流,速度快
fis = new FileInputStream(src);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream(target);
bos = new BufferedOutputStream(fos);
byte[] buf = new byte[1024];
int lenth = -1;
lenth = bis.read(buf);// 讀
while (lenth != -1) {
bos.write(buf, 0, lenth);// 寫
lenth = bis.read(buf);// 讀
}
// 先關高階流
// 實際上關閉高階流的同時,低階流也會被關掉了
bis.close();
bos.close();
fis.close();
fos.close();
}
}
FileInputStream——BufferedInputStream——DataInputStream一層包一層,不斷給管子加粗,提升讀寫速度
物件流ObjectInputStream 、ObjectOutputStream
只能將支援 java.io.Serializable 介面的物件寫入流中。每個 serializable 物件的類都被編碼,編碼內容包括類名和類簽名、物件的欄位值和陣列值,以及從初始物件中引用的其他所有物件的閉包。
writeObject 方法用於將物件寫入流中。所有物件(包括 String 和陣列)都可以通過 writeObject 寫入。可將多個物件或基元寫入流中。必須使用與寫入物件時相同的型別和順序從相應 ObjectInputstream 中讀回物件。
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* @author cuijiao 物件流,ObjectOutputStream可用於物件的持久化操作
*/
public class ObjectStream {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
// 寫物件
// writeObject();
// 讀物件
readObject();
}
/**
*
* 物件必須序列化並有序列號
*/
private static void readObject() {
// TODO Auto-generated method stub
File file = new File("D:\\data\\test.log");
if (!file.exists()) {
return;
}
FileInputStream fis = null;
ObjectInputStream ois = null;
Emp emp = null;
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
try {
emp = (Emp) ois.readObject();// 讀到最後無資料時,不是返回物件而是報EOFException
while (emp != null) {
System.out.println(emp);
emp = (Emp) ois.readObject();
}
} catch (EOFException e) {
// EOFException異常,表示讀資料結束,不需要任何操作
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 序列化,對於需要用writeObject寫入檔案的物件,必須要實現序列化
*/
private static void writeObject() {
File file = new File("D:\\data\\test.log");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
ObjectOutputStream oos = null;
Emp emp = new Emp("lisa", "e001", 888, true, new Date());
try {
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(emp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
其中需要讀寫的物件emp,必須實現序列化;
import java.io.Serializable;
import java.util.Date;
public class Emp implements Serializable {
/**
* defalut serialVersionUID
*/
private static final long serialVersionUID = 1L;
private String empName;
private String empNo;
private double salary;
private boolean sex;
private Date birthday;
/**
* default constructor
*/
public Emp() {
}
/**
* max constructor
*/
public Emp(String empName, String empNo, double salary, boolean sex, Date birthday) {
this.empName = empName;
this.empNo = empNo;
this.salary = salary;
this.sex = sex;
this.birthday = birthday;
}
/**
* min constructors
*/
public Emp(String empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Emp [ 姓名:" + empName + ", 員工號:" + empNo + ", 薪資:" + salary + ", 性別:" + (sex ? "男" : "女") + ", 生日:" + DateUtil.date2Sring(birthday) + "]";
}
}
java已有的類,大多已經實現了序列化,對於我們自己定義的類,需要實現序列號,並給預設的序列化即可。