⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notelocator.java

📁 手机记事本程序
💻 JAVA
字号:
/*
 * Created on 2005-3-3
 *
 * Note Project
 */
package com.favo.note;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Vector;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * @author Favo
 * 
 * NoteLocator < <life cycle>>
 */
public class NoteLocator {
	final public static int NEWNODE = -1;
	final private String STORENAME="newNoteStore"; 
	private static NoteLocator noteLocator = null;

	public static NoteLocator getInstance() {
		if (noteLocator == null) {
			noteLocator = new NoteLocator();
		}
		return noteLocator;
	}

	public Note getNote(int id) throws Exception {
		if (id == NEWNODE) {
			System.out.println("return new node");
			return new Note("", "", System.currentTimeMillis());
		} else {
			//Fake Date
			//			System.out.println("return fade node");
			//			return new Note("Example News",
			//					"the news should be read form the rms", System
			//							.currentTimeMillis());
			RecordStore rs = null;
			Note note = null;
			try {
				rs = RecordStore.openRecordStore(STORENAME, true);//open
				System.out.println("RecordStore open success");
				note = (Note) Note.deserialize(rs//deserialize
						.getRecord(id));
				System.out.println("-------debug note info--------\n" + note);
			} catch (Exception e2) {
				e2.printStackTrace();
				throw e2;
			} finally {
				if (rs != null) {
					try {
						rs.closeRecordStore();
					} catch (RecordStoreNotOpenException e1) {
						e1.printStackTrace();
					} catch (RecordStoreException e1) {
						e1.printStackTrace();
					}
				}
			}
			return note;
		}
	}

	public void saveNote(Note note, int id) throws Exception {
		RecordStore rs = null;
		byte[] noteByte = null;
		try {
			rs = RecordStore.openRecordStore(STORENAME, true);//open
			System.out.println("RecordStore open success");
			noteByte = note.serialize();
			if (id == NoteLocator.NEWNODE) {
				rs.addRecord(noteByte, 0, noteByte.length);
			} else {
				rs.setRecord(id, noteByte, 0, noteByte.length);
			}
			System.out.println("save success");
		} catch (Exception e2) {
			e2.printStackTrace();
			throw e2;
		} finally {
			if (rs != null) {
				try {
					rs.closeRecordStore();
				} catch (RecordStoreNotOpenException e1) {
					e1.printStackTrace();
				} catch (RecordStoreException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	public void deleteNote(int id) throws Exception {
		RecordStore rs = null;
		try {
			rs = RecordStore.openRecordStore(STORENAME, true);//open
			System.out.println("RecordStore open success");
			rs.deleteRecord(id);
			System.out.println("delete success");
		} catch (Exception e2) {
			e2.printStackTrace();
			throw e2;
		} finally {
			if (rs != null) {
				try {
					rs.closeRecordStore();
				} catch (RecordStoreNotOpenException e1) {
					e1.printStackTrace();
				} catch (RecordStoreException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	/*
	 * public void getNoteTitleList(String []titleArray,int []idArray) throws
	 * Exception { titleArray=new String[] { "New0", "New1", "New2" };
	 * idArray=new int[]{12,13,14}; }
	 */

	/**
	 * @param noteTitleList
	 * @param noteIdList
	 * @throws Exception
	 */
	public void getNoteInfoList(Vector noteTitleList, Vector noteIdList)
			throws Exception {
		RecordStore rs = null;
		try {
			rs = RecordStore.openRecordStore(STORENAME, true);//open
			System.out.println("RecordStore open success");
			RecordEnumeration re = rs.enumerateRecords(null, null, false);
			byte[] tempArray = new byte[0];
			while (re.hasNextElement()) {
				int recordId = re.nextRecordId();
				if (tempArray.length < rs.getRecordSize(recordId)) {
					tempArray = new byte[rs.getRecordSize(recordId)];
				}
				rs.getRecord(recordId, tempArray, 0);
				Note tempNote = (Note) Note.deserialize(tempArray);
				noteTitleList.addElement(tempNote.getTitle());
				noteIdList.addElement(new Integer(recordId));
			}
			System.out.println("get node info success");
		} catch (Exception e2) {
			throw e2;
		} finally {
			if (rs != null) {
				try {
					rs.closeRecordStore();
				} catch (RecordStoreNotOpenException e1) {
					e1.printStackTrace();
				} catch (RecordStoreException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	/**
	 * @author Favo
	 *
	 * Note <<Entity>>
	 * 有兴趣的朋友请注意对date的处理方法,一种保护性思想
	 */
	public static class Note {
		String title;

		String content;

		long datetime;

		/**
		 * @param title
		 * @param content
		 * @param date
		 */
		public Note(String title, String content, long datetime) {
			super();
			setTitle(title);
			setContent(content);
			setDate(datetime);
		}

		/**
		 *
		 */
		protected Note() {
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public Date getDate() {
			return new Date(datetime);
		}

		public void setDate(long datetime) {
			this.datetime = datetime;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

		public byte[] serialize() throws IOException {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(bos);
			dos.writeUTF(getTitle());
			dos.writeLong(datetime);
			dos.writeUTF(getContent());
			dos.flush();
			return bos.toByteArray();
		}

		public static Object deserialize(byte[] data) throws IOException {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			DataInputStream dis = new DataInputStream(bis);
			Note newNote = new Note();
			newNote.setTitle(dis.readUTF());
			newNote.setDate(dis.readLong());
			newNote.setContent(dis.readUTF());
			return newNote;
		}

		public String toString() {
			return "Title: " + getTitle() + "\nDate: " + getDate()
					+ "\nContent:" + getContent();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -