Android Open Source - bugfree-context-demo Note File A P I






From Project

Back to project page bugfree-context-demo.

License

The source code is released under:

MIT License

If you think the Android project bugfree-context-demo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hustbaidu.notebook.file;
/* w w  w  .  j av a 2s . c  om*/
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;

import com.hustbaidu.notebook.api.NoteBookInterface;
import com.hustbaidu.notebook.model.Note;

import android.content.Context;

public class NoteFileAPI implements NoteBookInterface {

  // ?activity????????
  private Context context;

  // ??note????
  private File noteDir = null;

  // ???????????
  private static NoteFileAPI mApi;

  // ???????format
  private SimpleDateFormat format;

  //
  private HashMap<String, Note> noteMap;

  private NoteFileAPI(Context context) {
    this.context = context;
    format = new SimpleDateFormat("yyyy?MM?dd? hh:mm:ss", Locale.CHINA);
  }

  /**
   * ????ApplicationContext?NoteAPi
   * 
   * @param context
   * @return
   */
  public static NoteFileAPI getInstance(Context context) {
    if (mApi != null) {
      mApi.context = context;
      return mApi;
    } else {
      mApi = new NoteFileAPI(context);
      mApi.noteDir = mApi.getNoteDir();
      return mApi;
    }
  }

  private File getNoteDir() {
    File file = this.context.getDir("notes", Context.MODE_PRIVATE);
    return file;
  }

  @Override
  public boolean save(Note note) {
    
    if(noteMap != null){
      if(!noteMap.containsKey(note.getTitle()))
        noteMap.put(note.getTitle(), note);
    } else {
      noteMap = new HashMap<String, Note>();
      noteMap.put(note.getTitle(), note);
    }
    
    try {
      File noteFile = new File(this.noteDir, note.getTitle());
      if(!noteFile.exists())
        noteFile.createNewFile();
      FileOutputStream outputStream = new FileOutputStream(noteFile);
      outputStream.write(note.getContent().getBytes());
      Date date = new Date();
      note.setModifyDate(format.format(date));
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  @Override
  public List<Note> findAll() {
    // TODO Auto-generated method stub

    if (noteMap != null) {
      ArrayList<Note> list = new ArrayList<Note>();
      for (String key : noteMap.keySet()) {
        list.add(noteMap.get(key));
      }
      return list;
    } else {
      noteMap = new HashMap<String, Note>();
    }
    try {
      File[] files = noteDir.listFiles();
      Date date = new Date();
      Note note;
      StringBuffer content;
      Scanner scanner;
      ArrayList<Note> notes = new ArrayList<Note>(files.length);
      for (File file : files) {
        note = new Note();
        content = new StringBuffer();
        content.toString();
        note.setTitle(file.getName());
        date.setTime(file.lastModified());
        note.setModifyDate(format.format(date));
        scanner = new Scanner(file);
        while (scanner.hasNext()) {
          content.append(scanner.next());
        }
        note.setContent(content.toString());
        scanner.close();
        notes.add(note);
        noteMap.put(note.getTitle(), note);
        return notes;
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return null;
    }
    return null;
  }

  /**
   * ?????????????????API
   * 
   * @return null
   */
  @Deprecated
  @Override
  public Note findWithId(int id) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Note findWithName(String name) {
    // TODO Auto-generated method stub
    if (name == null || name.equals("")) {
      return null;
    }

    if (noteMap != null && noteMap.containsKey(name)) {
      return noteMap.get(name);
    }

    Note note = new Note();
    try {
      File file = new File(noteDir, name);
      note.setTitle(name);
      Scanner scanner = new Scanner(file);
      StringBuffer buffer = new StringBuffer();
      while (scanner.hasNext()) {
        buffer.append(scanner.next());
      }
      scanner.close();
      note.setContent(buffer.toString());
      Date date = new Date();
      date.setTime(file.lastModified());
      note.setModifyDate(format.format(date));

      noteMap.put(name, note);
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return null;
    }
    return note;
  }

  @Override
  public boolean remove(Note note) {
    // TODO Auto-generated method stub

    if (noteMap.containsKey(note.getTitle()))
      noteMap.remove(note);
    try {
      File file = new File(noteDir, note.getTitle());
      if (file.exists()) {
        file.delete();
        return true;
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return false;
    }
    return false;
  }

}




Java Source Code List

com.hustbaidu.notebook.activity.EditorActivity.java
com.hustbaidu.notebook.activity.MainActivity.java
com.hustbaidu.notebook.api.NoteBookInterface.java
com.hustbaidu.notebook.file.NoteFileAPI.java
com.hustbaidu.notebook.model.Note.java