📄 note_diary.java
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Stack;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
/*
* 创建日期 2006-12-9
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Note_diary {//保存负责日记,日程管理的各方法的具体实现,后台逻辑运算
/**
* @param name
* @throws Exception
*/
static Note_diary one;
static RecordStore note_diary;
static String aname;
static int ayear,amonth,aday,awhich;
/**
* @return 返回 aday。
* @throws
*/
public Note_diary() {
try {
note_diary=RecordStore.openRecordStore("Note_diary_Store",true);
} catch (RecordStoreException e) {
e.printStackTrace();
}
if(empty(note_diary)){
try {
About about=new About();
about.set_defalt1();
} catch (IOException e1) {
e1.printStackTrace();
}
}
one=this;
// TODO 自动生成构造函数存根
}
private boolean empty(RecordStore rs){
try {
if(note_diary.getNumRecords()==0){
return true;
}
} catch (RecordStoreNotOpenException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return false;
}
static boolean add(date adate) {
try {
if(add(adate.getYear(),adate.getMonth(),adate.getDay(),adate.getName(),adate.getContent(),adate.getWhich()))
return true;
} catch (IOException e) {
return false;
}
return false;
}
private static date decode(byte[] data) throws Exception{
date adate=new date();
ByteArrayInputStream byteInputStream =new ByteArrayInputStream(data);
DataInputStream dataInputStream=new DataInputStream(byteInputStream);
adate.setYear(dataInputStream.readInt());
adate.setMonth(dataInputStream.readInt());
adate.setDay(dataInputStream.readInt());
adate.setName(dataInputStream.readUTF());
adate.setContent(dataInputStream.readUTF());
adate.setWhich(dataInputStream.readInt());
dataInputStream.close();
byteInputStream.close();
return adate;
}
private byte[] encode(int year,int month,int day,String name,String content,int which){
byte[] recordOut=null;
try{
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);
dataOutputStream.writeInt(year);
dataOutputStream.writeInt(month);
dataOutputStream.writeInt(day);
dataOutputStream.writeUTF(name);
dataOutputStream.writeUTF(content);
dataOutputStream.writeInt(which);
dataOutputStream.flush();
recordOut = byteOutputStream.toByteArray();
dataOutputStream.close();
byteOutputStream.close();
}catch(Exception e){
}
return recordOut;
}
//which 1为重要日子,2为提醒,3为日记
static boolean add(int year,int month,int day,String name,String content,int which) throws IOException{
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);
dataOutputStream.writeInt(year);
dataOutputStream.writeInt(month);
dataOutputStream.writeInt(day);
dataOutputStream.writeUTF(name);
dataOutputStream.writeUTF(content);
dataOutputStream.writeInt(which);
dataOutputStream.flush();
byte[] recordOut = byteOutputStream.toByteArray();
try {
int newRecordId = note_diary.addRecord(recordOut, 0, recordOut.length);
return true;
} catch (RecordStoreException e) {
e.printStackTrace();
}
dataOutputStream.close();
byteOutputStream.close();
return false;
}
class Filter1 implements RecordFilter{
public boolean matches(byte[]data){
date adate=new date();
try {
adate=decode(data);
if(adate.getWhich()==awhich)
return true;
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return false;
}
}
class Filter2 implements RecordFilter{
public boolean matches(byte[]data){
date adate=new date();
try {
adate=decode(data);
if(adate.getWhich()==awhich || adate.getWhich()==awhich+4)
return true;
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return false;
}
}
class Filter implements RecordFilter{
public boolean matches(byte[]data){
date adate;
try {
adate=decode(data);
if(adate.getMonth()==amonth&&adate.getDay()==aday)
return true;
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return false;
}
}
class Compare1 implements RecordComparator{
public int compare(byte[] a,byte[] b){
date adate1,adate2;
try {
adate1=decode( a);
adate2=decode( b);
if(adate1.year >adate2.year )
return RecordComparator.PRECEDES;
else if(adate1.year <adate2.year )
return RecordComparator.FOLLOWS ;
else {
if(adate1.month >adate2.month )
return RecordComparator.PRECEDES;
else if(adate1.month <adate2.month )
return RecordComparator.FOLLOWS ;
else{
if(adate1.day >adate2.day )
return RecordComparator.PRECEDES;
else if(adate1.day <adate2.day )
return RecordComparator.FOLLOWS ;
else
return RecordComparator.EQUIVALENT ;
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
return 0;
}
}
static int get_num(int which){
awhich=which;
RecordEnumeration enum=null;
if(which==1 || which ==2){
Compare1 comp=one.new Compare1();
Filter1 f=one.new Filter1();
try {
enum = note_diary.enumerateRecords(f,null,true);
} catch (RecordStoreNotOpenException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
return 0;
}
}
else{
Compare1 comp=one.new Compare1();
Filter2 f=one.new Filter2();
try {
enum = note_diary.enumerateRecords(f,null,true);
} catch (RecordStoreNotOpenException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
return 0;
}
}
return enum.numRecords();
}
static date[] getdiary() throws Exception, RecordStoreException{
date[] dates=null;
Stack s=new Stack();
int index=0;
for(int i=1;i<note_diary.getNextRecordID();i++){
try{
date a=new date();
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(note_diary.getRecord(i));
DataInputStream dataInputStream = new DataInputStream(byteInputStream);
a.setYear(dataInputStream.readInt());
a.setMonth(dataInputStream.readInt());
a.setDay(dataInputStream.readInt());
a.setName(dataInputStream.readUTF());
a.setContent(dataInputStream.readUTF());
a.setWhich(dataInputStream.readInt());
if(3 ==a.which || 7==a.which){
s.push(a);
index++;
dataInputStream.close();
byteInputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
System.out .println("数目") ;
System.out .println(index) ;
if(!s.empty()){
dates=new date[s.size()];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -