?? backupimpl.java
字號:
package org.com.gather;
import java.util.*;
import java.io.*;
public class BackUpImpl implements BackUp{
private Properties pro = null;
private Log log = null;
public BackUpImpl(Properties pro){
this.pro = pro;
}
public void store(Collection col){
File file = new File(pro.getProperty("backUpFile"));
try{
file.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
ObjectOutputStream oos = null;
try{
Collection c = this.load();
if(!c.isEmpty()){
log.writeDebug("之前有備份數據!");
c.addAll(col);
col = c;
}
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(col);
log.writeDebug("成功將數據寫入到備份文件!");
}catch(IOException e){
e.printStackTrace();
log.writeDebug("寫入備份文件失敗!");
}
}
public void storeForEmpty(Collection col){
File file = new File(pro.getProperty("backUpFile"));
try{
file.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
ObjectOutputStream oos = null;
if(!col.isEmpty()){
try{
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(col);
log.writeDebug("再次采集到的數據為空,把備份文件再次寫回到文件;");
}catch(IOException e){
e.printStackTrace();
}
}
}
public Collection load(){
File file = new File(pro.getProperty("backUpFile"));
try{
file.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
ObjectInputStream ois = null;
if (file.length() < 1) {
//如果長度為0表示該文件為新建的.這個時候
//用readObject()就會報EOFException
log.writeDebug("備份文件為空!");
return new ArrayList();
}else{
try{
ois = new ObjectInputStream(new FileInputStream(file));
Object o = ois.readObject();
if(o != null){
Collection col = (Collection)o;
return col;
}else{
return new ArrayList();
}
}catch(FileNotFoundException e){
e.printStackTrace();
return null;
}catch(IOException e){
e.printStackTrace();
return null;
}catch(ClassNotFoundException e){
e.printStackTrace();
return null;
}
}
}
public void clear(){
File file = new File(pro.getProperty("backUpFile"));
OutputStream os = null;
ObjectOutputStream oos = null;
try{
os = new FileOutputStream(file);
oos = new ObjectOutputStream(os);
oos.writeObject(new ArrayList());
}catch(IOException e){
e.printStackTrace();
log.writeDebug("清空備份文件失敗;");
}
}
public void setLog(Log log){
this.log = log;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -