?? recorddaobyfile.java
字號(hào):
/**
* @(#)dao.RecordDAOByFile.java 2008-9-2
* Copy Right Information : Tarena
* Project : JavaQQ
* JDK version used : jdk1.6.4
* Comments : 記錄操作類。
* Version : 1.0
* Sr Date Modified By Why & What is modified
* 1. 2008-9-2 小豬 新建
**/
package dao;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.Vector;
import data.Record;
/**
* 記錄操作類,以文件的方式處理Record。
* 2008-9-2
* @author 達(dá)內(nèi)科技[Tarena Training Group]
* @version 1.0
* @since JDK1.6(建議)
*/
public class RecordDAOByFile implements DAO<Record, Integer> {
private String path = "logs";
private String suffixName = ".dat";
private String pathAdmin = "records";
/**
* 添加一條新記錄。
* @param record 記錄。
* @throws FileNotFoundException
* @throws IOException
* @return 添加成功否。
*/
public boolean add(Record record) throws FileNotFoundException, IOException{
File category = new File(record.getToid()+File.separator+path);
if(!category.exists())
category.mkdirs();
File file = new File(record.getToid()+File.separator+path+File.separator+record.getFromid()+suffixName);
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
oos.writeObject(record);
oos.flush();
oos.close();
oos = null;
return true;
}
/**
* 管理員添加一條新記錄。
* @param record 記錄。
* @return 返回添加成功否。
* @throws FileNotFoundException
* @throws IOException
*/
public boolean addRecordForAdmin(Record record) throws FileNotFoundException, IOException{
File category = new File(pathAdmin);
if(!category.exists())
category.mkdir();
File file = new File(pathAdmin+File.separator+record.getToid()+suffixName);
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
oos.writeObject(record);
oos.flush();
oos.close();
oos = null;
return true;
}
/**
* 刪除一條記錄。
* @param record 要?jiǎng)h除的記錄。
* @return 返回刪除成功否。
*/
public boolean delete(Record record) throws Exception {
File file = new File(record.getToid()+File.separator+path+File.separator+record.getFromid()+suffixName);
if(file.exists())
return file.delete();
else
return false;
}
/**
* 查找所有記錄。
* @throws Exception
* @return 無(wú)論如何都返回空。未作處理。
*/
public Vector<Record> findAll() throws Exception {
return null;
}
/**
* 查找好友發(fā)送的留言記錄。
* @param jqnum 該用戶的jqmum
* @return 返回該用戶的留言記錄
* @throws FileNotFoundException
* @throws IOException
*/
public Vector<Record> findLeaveRecord(int jqnum) throws FileNotFoundException, IOException{
File file = new File(pathAdmin+File.separator+jqnum+suffixName);
if(!file.exists())
return null;
else{
Vector<Record> v = new Vector<Record>();
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
while(true){
Object obj = null;
try {
obj = ois.readObject();
} catch (IOException e) {
break;
} catch (ClassNotFoundException e) {
break;
}
if(obj==null)
break;
if(obj instanceof Record){
Record record = (Record)obj;
record.setRead(true);
record.setReadTime(new Date());
v.add(record);
}
}
ois.close();
ois = null;
return v;
}
}
/**
* 刪除管理員的記錄。
* @param jqnum
* @return 刪除成功否。
*/
public boolean deleteRecordForAdmin(int jqnum){
File file = new File(pathAdmin+File.separator+jqnum+suffixName);
if(file.exists())
return file.delete();
else
return false;
}
/**
* 按id查找記錄
* @param id 記錄的id
* @throws Exception
* @return 無(wú)論如何都返回空。未作處理。
*/
public Record findById(Integer id) throws Exception {
return null;
}
/**
* 更新記錄。
* @param record 欲跟新的記錄。
* @throws Exception
* @return 無(wú)論如何都返回false,未作處理。
*/
public boolean update(Record record) throws Exception {
return false;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -