Java多文本文件行迭代与指定字段提取问题求助
解决方案:从文本文件提取数据封装到Java对象
首先,假设你已经有了Person和Clothing两个自定义对象(以下是示例结构,和你的实现对齐即可):
class Clothing { private String name; private String description; public Clothing(String name, String description) { this.name = name; this.description = description; } // Getter和Setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } class Person { private String name; private List<Clothing> clothingList; public Person(String name) { this.name = name; this.clothingList = new ArrayList<>(); } // Getter和Setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Clothing> getClothingList() { return clothingList; } public void addClothing(Clothing clothing) { this.clothingList.add(clothing); } }
接下来,替换原有读取文件的逻辑,改为逐行解析并封装对象的实现:
import java.io.*; import java.util.*; public class ReadFile { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(System.in); System.out.print("Enter file path: "); String fileName = input.nextLine(); input.close(); Person person = parseFileToPerson(fileName); // 测试输出验证结果 if (person != null) { System.out.println("Person Name: " + person.getName()); for (Clothing clothing : person.getClothingList()) { System.out.println("\nClothing Name: " + clothing.getName()); System.out.println("Description: " + clothing.getDescription()); } } } public static Person parseFileToPerson(String fileName) throws FileNotFoundException { File newFile = new File(fileName); if (!newFile.canRead()) { System.out.println("No file found. Please try again."); return null; } Person person = null; String currentClothingName = null; StringBuilder currentDescription = new StringBuilder(); boolean inClothingSection = false; boolean collectingDescription = false; try (Scanner scanFile = new Scanner(newFile)) { while (scanFile.hasNextLine()) { String line = scanFile.nextLine().trim(); if (line.isEmpty()) continue; // 跳过空行 // 读取首行作为姓名 if (person == null) { person = new Person(line); continue; } // 进入衣物区块:跳过----直到找到Clothing: if (!inClothingSection) { if (line.equals("----")) { while (scanFile.hasNextLine()) { line = scanFile.nextLine().trim(); if (line.equals("Clothing:")) { inClothingSection = true; break; } } } continue; } // 处理衣物内容 if (line.equals("****")) { // 保存当前衣物 if (currentClothingName != null && currentDescription.length() > 0) { person.addClothing(new Clothing(currentClothingName, currentDescription.toString().trim())); // 重置状态 currentClothingName = null; currentDescription.setLength(0); collectingDescription = false; } } else if (line.equals("----")) { // 结束衣物区块解析 break; } else { if (!collectingDescription) { // 读取衣物名称 currentClothingName = line; collectingDescription = true; } else { // 拼接描述文本 currentDescription.append(line).append(" "); } } } // 处理最后一个未被****收尾的衣物 if (currentClothingName != null && currentDescription.length() > 0) { person.addClothing(new Clothing(currentClothingName, currentDescription.toString().trim())); } } catch (Exception e) { e.printStackTrace(); } return person; } }
核心逻辑说明
- 状态管理:用布尔变量标记当前解析阶段(未读取姓名、未进入衣物区块、正在收集描述),避免复杂嵌套判断
- 逐行解析:不再拼接全部文本,而是边读边处理,内存效率更高
- 边界处理:
- 跳过空行和无关分隔线
- 处理文件末尾无****的情况,确保最后一件衣物被保存
- 自动识别衣物区块的开始和结束
内容的提问来源于stack exchange,提问作者Nova Sanitas
相关产品推荐
相关产品推荐

