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

📄 reminddao.java

📁 用JAVA开发的提醒精灵
💻 JAVA
字号:
package com.liming.remind.dao;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.liming.remind.dto.Remind;
import com.liming.remind.exception.MyArrayIndexOutOfBoundsException;
import com.liming.remind.exception.MyFileNotFoundException;
import com.liming.remind.exception.MyIOException;


/**
 * Function : The persistent operation about Remind object
 * 
 * @author Liming
 * @time Dec 16, 2008 9:43:48 PM
 * @version 1.0
 */
public class RemindDAO {

    private File file;

    private List<Remind> remindsList;

    public RemindDAO() throws MyFileNotFoundException, MyIOException,
            MyArrayIndexOutOfBoundsException {
        file = new File("res\\remind");
        remindsList = this.findAllReminds();
    }

    /**
     * @return the remindsList
     */
    public List<Remind> getRemindsList() {
        return remindsList;
    }

    /**
     * Function : Add one Remind
     * 
     * @param remind
     * @return
     * @throws MyFileNotFoundException
     * @throws MyIOException
     */
    public boolean addRemind(Remind remind) throws MyFileNotFoundException,
            MyIOException {
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            String s = remind.getId() + "." + remind.getTime() + "-"
                    + remind.getTips() + "\n";
            fos.write(s.getBytes());
            fos.flush();
            fos.close();
            remindsList.add(remind);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new MyFileNotFoundException("MyFileNotFoundException");
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyIOException("MyIOException");
        }
    }

    /**
     * Function : Delete one Remind
     * 
     * @param id
     * @return
     * @throws MyFileNotFoundException
     * @throws MyIOException
     * @throws MyFileNotFoundException
     * @throws MyIOException
     */
    public boolean deleteRemind(Remind remind) throws MyFileNotFoundException,
            MyIOException {
        remindsList.remove(remind);
        this.reCoveredAllReminds(remindsList);
        return true;
    }

    /**
     * Function : Delete all Reminds
     * 
     * @return
     * @throws MyFileNotFoundException
     * @throws MyIOException
     */
    public boolean deleteAllReminds() throws MyFileNotFoundException,
            MyIOException {
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write("".getBytes());
            fos.flush();
            fos.close();
            remindsList.clear();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new MyFileNotFoundException("MyFileNotFoundException");
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyIOException("MyIOException");
        }
    }

    /**
     * Function : Find all reminds and put them to List
     * 
     * @return
     * @throws MyFileNotFoundException
     * @throws MyIOException
     * @throws MyArrayIndexOutOfBoundsException
     */
    private List<Remind> findAllReminds() throws MyFileNotFoundException,
            MyIOException, MyArrayIndexOutOfBoundsException {
        try {
            FileInputStream fis = new FileInputStream(file);
            byte b[] = new byte[1024];

            int len = fis.read(b);
            if (len == -1) {
                return new ArrayList<Remind>();
            }
            String s = new String(b, 0, len);
            fis.close();
            List<Remind> list = this.splitStringToArrayByN(s);
            return list;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new MyFileNotFoundException("MyFileNotFoundException");
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyIOException("MyIOException");
        }
    }

    /**
     * Function : ReCovered all reminds with Reminds List which have provided
     * 
     * @param reminds
     * @return
     * @throws MyFileNotFoundException
     * @throws MyIOException
     */
    private boolean reCoveredAllReminds(List<Remind> reminds)
            throws MyFileNotFoundException, MyIOException {
        // Clear all reminds in file
        try {
            FileOutputStream clear = new FileOutputStream(file);
            clear.write("".getBytes());
            clear.flush();
            clear.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new MyFileNotFoundException("MyFileNotFoundException");
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyIOException("MyIOException");
        }

        // Add all Reminds in List
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            for (int i = 0;i < reminds.size();i++) {
                Remind remind = reminds.get(i);
                String s = remind.getId() + "." + remind.getTime() + "-"
                        + remind.getTips() + "\n";
                fos.write(s.getBytes());
            }
            fos.flush();
            fos.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new MyFileNotFoundException("MyFileNotFoundException");
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyIOException("MyIOException");
        }
    }

    /**
     * Function : Split String to Array by '\n' character
     * 
     * @param s
     * @return
     * @throws MyArrayIndexOutOfBoundsException
     */
    private List<Remind> splitStringToArrayByN(String s)
            throws MyArrayIndexOutOfBoundsException {
        String rem[] = new String[24];
        int index = 0;
        int len = 0;
        for (int i = 0;i < s.length();i++) {
            if (s.charAt(i) == '\n') {
                try {
                    rem[len] = s.substring(index, i);
                } catch (ArrayIndexOutOfBoundsException e) {
                    e.printStackTrace();
                    throw new MyArrayIndexOutOfBoundsException(
                            "MyArrayIndexOutOfBoundsException");
                }
                index = i + 1;
                len++;
            }
        }
        return this.parseStringArrayToReminds(rem);
    }

    /**
     * Function : Parse the String Array to Reminds
     * 
     * @param rem
     * @return
     */
    private List<Remind> parseStringArrayToReminds(String[] rem) {
        List<Remind> list = new ArrayList<Remind>();
        Remind remind;
        String s;
        int remLen = rem.length;
        for (int i = 0;i < remLen;i++) {
            s = rem[i];
            if (s != null) {
                remind = new Remind();
                String sId = s.substring(0, s.indexOf('.'));
                remind.setId(Integer.parseInt(sId));
                remind.setTime(s.substring(s.indexOf('.') + 1, s.indexOf('-')));
                remind.setTips(s.substring(s.indexOf('-') + 1));
                list.add(remind);
            }
        }
        return list;
    }

}

⌨️ 快捷键说明

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