Java实现TXT指定行覆盖与指定日期数据查询方案咨询
Java TXT文件日期数据的覆盖与查询实现方案
需求背景
现有Java程序可将日期+3条附属数据(fridge1、fridge2、info)按行追加到TXT文件,当前需补充两个核心功能:
- 输入相同日期时,覆盖该日期对应的后续3行数据
- 根据指定日期查询对应的数据
以下是针对数据定位问题的具体实现方案。
核心实现思路
由于TXT文件为流式存储,无法直接修改指定位置内容,因此采用内存缓存全量内容的方式处理数据定位与操作:每个数据块固定为4行(日期行 + fridge1行 + fridge2行 + info行),以此为单位进行定位和修改。
1. 相同日期数据覆盖功能实现
修改saveData方法,逻辑如下:
- 读取文件所有行到
List<String>中 - 遍历列表查找目标日期的行索引
- 若找到目标日期,替换该索引开始的4行为新数据的4行;若未找到,则追加新数据
- 将修改后的列表重新写入文件
2. 指定日期数据查询功能实现
新增queryData静态方法,逻辑如下:
- 遍历文件每一行,匹配目标日期
- 匹配成功后,读取接下来的3行数据,封装为
Test对象返回 - 若遍历结束未找到,返回
null
完整代码实现
Test类(含覆盖与查询逻辑)
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { public String date; public String fridge1; public String fridge2; public String info; public Test(String date, String fridge1, String fridge2, String info) { this.date = date; this.fridge1 = fridge1; this.fridge2 = fridge2; this.info = info; } // Getter & Setter public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getFridge1() { return fridge1; } public void setFridge1(String fridge1) { this.fridge1 = fridge1; } public String getFridge2() { return fridge2; } public void setFridge2(String fridge2) { this.fridge2 = fridge2; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Override public String toString() { return date + "\n" + fridge1 + "\n" + fridge2 + "\n" + info + "\n"; } public void saveData() { File file = new File("text.txt"); List<String> allLines = new ArrayList<>(); boolean dateExists = false; int targetIndex = -1; // 读取文件所有行到列表 try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { allLines.add(scanner.nextLine()); } } catch (FileNotFoundException e) { // 文件不存在时直接跳过读取流程 } // 查找目标日期的行索引 for (int i = 0; i < allLines.size(); i++) { if (allLines.get(i).equals(date)) { dateExists = true; targetIndex = i; break; } } if (dateExists) { // 替换目标日期开始的4行数据 allLines.set(targetIndex, date); if (targetIndex + 1 < allLines.size()) allLines.set(targetIndex + 1, fridge1); else allLines.add(fridge1); if (targetIndex + 2 < allLines.size()) allLines.set(targetIndex + 2, fridge2); else allLines.add(fridge2); if (targetIndex + 3 < allLines.size()) allLines.set(targetIndex + 3, info); else allLines.add(info); } else { // 追加新数据 allLines.add(date); allLines.add(fridge1); allLines.add(fridge2); allLines.add(info); } // 将修改后的列表写入文件 try (PrintWriter writer = new PrintWriter(file)) { for (String line : allLines) { writer.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } } // 静态查询方法,根据日期返回对应Test对象 public static Test queryData(String targetDate) { File file = new File("text.txt"); try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.equals(targetDate)) { // 读取后续3行数据,处理文件末尾边界情况 String fridge1 = scanner.hasNextLine() ? scanner.nextLine() : ""; String fridge2 = scanner.hasNextLine() ? scanner.nextLine() : ""; String info = scanner.hasNextLine() ? scanner.nextLine() : ""; return new Test(targetDate, fridge1, fridge2, info); } } } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } }
Main类(测试代码)
public class Main { public static void main(String[] args) { // 测试覆盖功能:先写入初始数据,再写入相同日期的更新数据 Test test1 = new Test("19/02/1992", "10", "9", "ok"); test1.saveData(); Test test1Update = new Test("19/02/1992", "15", "12", "updated"); test1Update.saveData(); // 测试查询功能 Test queriedData = Test.queryData("19/02/1992"); if (queriedData != null) { System.out.println("查询到的数据:"); System.out.println(queriedData.toString()); } else { System.out.println("未找到指定日期的数据"); } } }
内容的提问来源于stack exchange,提问作者Cineczeq
相关产品推荐
相关产品推荐

