树形文件读取和导出

该程序从指定文件读取数据,将每一行转化为UserExampleExam对象,然后基于id和managerId建立父-子层级关系。处理后,将结果以特定格式写入新的文件。主要涉及数据的遍历、过滤、转换和递归操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.liuyang.service;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class test {
    public static void main(String[] args) {
        // step 1: read the file
        try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\86150\\Desktop\\test\\exam-data-2022.txt"))) {
            String line;
            /* each line of the file will be converted into an object and stored in a list */
            List<UserExampleExam> userExampleExams = new ArrayList<>();

            // step 2: read each line of the file
            while ((line = br.readLine()) != null) {
                // step 3: separate each line with a comma to form a new array
                String[] data = line.split(",");
                // step 4: encapsulate the array as an object
                UserExampleExam userExampleExam = DataStitching(data.length, data);
                // add the object to the list
                userExampleExams.add(userExampleExam);
            }

            // step 5: list parent-child relationships based on id and managerId
            List<UserExampleExam> newUserExampleExams = LoopOutput(userExampleExams);

            // step 6: format the result style and write to the result file
            output(newUserExampleExams);

        } catch (IOException e) {
            System.err.println("Failed to read file: " + e.getMessage());
        }
    }

    /**
     * file output
     * @param
     */
    private static void output(List<UserExampleExam> data) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Us
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值