答案:该Java笔记应用通过Note类存储标题、内容和创建时间,NoteManager类实现增删改查及文件持久化,主程序提供命令行菜单交互,数据序列化保存至本地notes.dat文件。

开发一个简易的Java笔记应用,核心是实现基本的增删改查功能,并将数据持久化到本地文件。整个项目不需要复杂的框架,适合初学者练习面向对象编程和文件操作。
1. 设计笔记类(Note)
每个笔记可以包含标题、内容和创建时间。定义一个简单的POJO类来表示单条笔记。
import java.time.LocalDateTime;public class Note { private String title; private String content; private LocalDateTime createTime; public Note(String title, String content) { this.title = title; this.content = content; this.createTime = LocalDateTime.now(); } // Getter 和 Setter 方法 public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public LocalDateTime getCreateTime() { return createTime; } @Override public String toString() { return "标题:" + title + "n内容:" + content + "n创建时间:" + createTime + "n"; }}
2. 实现笔记管理类(NoteManager)
负责管理所有笔记的添加、删除、查询和保存操作。使用ArrayList存储笔记列表,并通过ObjectOutputStream/ObjectInputStream实现序列化读写。
import java.io.*;import java.util.ArrayList;import java.util.List;public class NoteManager implements Serializable { private List notes; private static final String FILE_PATH = "notes.dat"; public NoteManager() { notes = new ArrayList(); loadNotes(); // 启动时加载已保存的笔记 } public void addNote(Note note) { notes.add(note); saveNotes(); // 添加后立即保存 } public boolean removeNote(String title) { boolean removed = notes.removeIf(note -> note.getTitle().equals(title)); if (removed) { saveNotes(); } return removed; } public List getAllNotes() { return new ArrayList(notes); // 返回副本避免外部修改 } public Note findNoteByTitle(String title) { return notes.stream() .filter(note -> note.getTitle().equals(title)) .findFirst() .orElse(null); } private void saveNotes() { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_PATH))) { oos.writeObject(notes); } catch (IOException e) { System.err.println("保存失败:" + e.getMessage()); } } @SuppressWarnings("unchecked") private void loadNotes() { File file = new File(FILE_PATH); if (!file.exists()) return; try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { notes = (List) ois.readObject(); } catch (IOException | ClassNotFoundException e) { System.err.println("加载失败:" + e.getMessage()); } }}
3. 创建主程序界面(命令行交互)
使用Scanner接收用户输入,提供简单菜单驱动的操作流程。
立即学习“Java免费学习笔记(深入)”;
import java.util.List;import java.util.Scanner;public class Main { public static void main(String[] args) { NoteManager manager = new NoteManager(); Scanner scanner = new Scanner(System.in); boolean running = true; System.out.println("欢迎使用简易笔记应用!"); while (running) { System.out.println("n请选择操作:"); System.out.println("1. 查看所有笔记"); System.out.println("2. 添加笔记"); System.out.println("3. 删除笔记"); System.out.println("4. 按标题查找"); System.out.println("5. 退出"); System.out.print("输入选项:"); int choice = scanner.nextInt(); scanner.nextLine(); // 消费换行符 switch (choice) { case 1: List allNotes = manager.getAllNotes(); if (allNotes.isEmpty()) { System.out.println("暂无笔记。"); } else { for (Note note : allNotes) { System.out.println("---n" + note); } } break; case 2: System.out.print("请输入标题:"); String title = scanner.nextLine(); System.out.print("请输入内容:"); String content = scanner.nextLine(); manager.addNote(new Note(title, content)); System.out.println("笔记已添加!"); break; case 3: System.out.print("请输入要删除的标题:"); String delTitle = scanner.nextLine(); if (manager.removeNote(delTitle)) { System.out.println("删除成功!"); } else { System.out.println("未找到该标题的笔记。"); } break; case 4: System.out.print("请输入要查找的标题:"); String searchTitle = scanner.nextLine(); Note found = manager.findNoteByTitle(searchTitle); if (found != null) { System.out.println("---n" + found); } else { System.out.println("未找到该笔记。"); } break; case 5: running = false; System.out.println("再见!"); break; default: System.out.println("无效选项,请重试。"); } } scanner.close(); }}
基本上就这些。运行程序后,笔记会自动保存到项目目录下的notes.dat文件中,下次启动时可继续使用。如果想扩展功能,可以加入编辑笔记、按时间排序或导出为文本文件等功能。不复杂但容易忽略的是异常处理和资源关闭,记得始终用try-with-resources确保流正确释放。
以上就是在Java中如何开发简易笔记应用的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/12554.html
微信扫一扫
支付宝扫一扫