用ArrayList+BlogPost类实现内存版CRUD:id用long、时间用LocalDateTime、重写equals/hashCode;updateById需空指针检查;输入统一用nextLine()并解析;文件操作用Files.write确保UTF-8和目录创建。
ArrayList + 简单类结构快速搭起CRUD骨架不需要数据库也能跑通完整流程,适合练手。核心是把每篇博客抽象成 BlogPost 类,用 ArrayList 当内存数据库。
关键点:字段别用 String 堆砌,要封装可读性——比如 id 用 long、createdAt 用 LocalDateTime,否则后期加时间范围查询会卡死。
BlogPost 必须重写 equals() 和 hashCode(),否则 list.remove(post) 可能删不掉System.out.print
ln("当前共 " + posts.size() + " 篇文章"),避免误以为没生效AtomicLong 或静态变量自增,防止重复public class BlogPost {
private long id;
private String title;
private String content;
private LocalDateTime createdAt;
public BlogPost(long id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
this.createdAt = LocalDateTime.now();
}
// getter/setter 省略,但必须有
}
updateById() 时最容易漏掉的空指针检查用户输入 ID 查不到对应文章,直接调 post.setTitle(newTitle) 就抛 NullPointerException。这不是逻辑错,是防御缺失。
真实场景里,ID 输入错误、前端传参丢失、URL 路径参数被截断都可能导致 findPostById() 返回 null。
false 或抛自定义异常(如 PostNotFoundException),不要继续执行System.exit(1) 或打印堆栈后沉默——这会让调用方无法区分“没找到”和“系统崩了”Optional,注意 optional.orElse(null) 又绕回空指针,要用 optional.ifPresent(...)
Scanner.nextLine() 套娃用户输完标题按回车,再输内容时 nextLine() 经常读到空字符串——因为前一个 nextInt() 或 next() 没吃掉换行符。
统一用 nextLine(),对数字字段再解析:
Integer.parseInt(scanner.nextLine().trim()),并包 try-catch NumberFormatException
content.trim().isEmpty() 要提示“内容不能为空”"1".equals(choice)),别用 switch (choice),避免输入字母崩掉直接写 new FileWriter("posts.txt") 在 Windows 上可能乱码,IDE 运行和 jar 包运行路径也不同。
Files.write(Paths.get("data/posts.json"), jsonBytes, StandardCharsets.UTF_8)
Files.createDirectories(Paths.get("data")),否则 Linux/macOS 下直接报 No such file or directory
Gson 库:new Gson().toJson(posts, List.class),别拼字符串复杂点在于:纯内存版没有事务,导出中途崩溃会导致文件损坏;真要可靠,得加临时文件 + 原子重命名,但小项目先跳过。