You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java读取TXT文件重复显示初始账户问题求助

Java读取TXT账户文件重复显示首次查询结果的问题

程序功能:输入账户编号查询对应账户信息(账号、姓氏、余额),输入9999退出。首次查询可正常显示目标账户,但后续输入其他账号时,始终显示首次查询的账户信息,添加打印语句排查仍未找到原因。

代码实现

import java.nio.file.*;
import static java.nio.file.StandardOpenOption.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Scanner;

public class ReadBankAccountsRandomly {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Path custRecords = Paths.get("D:\\School\\2nd Sem\\OOP\\Module 7\\RochesterBankCustomerRecords.txt");
        final String ACCOUNT_NUM = "0000";
        final String LAST_NAME_FORMAT = "        ";
        final int NAME_LENGTH = LAST_NAME_FORMAT.length();
        final String BALANCE_FORMAT = "00000.00";
        String delimiter = ",";
        String s = ACCOUNT_NUM + delimiter + LAST_NAME_FORMAT + delimiter + BALANCE_FORMAT + System.getProperty("line.separator");
        final int RECSIZE = s.length();
        FileChannel fcCR = null;
        String findAccount;
        int accountNum;

        byte data[] = s.getBytes();
        final String EMPTY_ACCT = "0000";
        final String QUIT = "9999";
        String[] array;

        try {
            fcCR = (FileChannel) Files.newByteChannel(custRecords, READ);
            ByteBuffer buffer = ByteBuffer.wrap(data);
            System.out.println("\nEnter account to display >> ");
            findAccount = input.nextLine();

            while (!findAccount.equals(QUIT)) {
                accountNum = Integer.parseInt(findAccount);
                System.out.println(accountNum);
                fcCR.position((long) accountNum * RECSIZE);
                System.out.println(fcCR.position());
                // 关键修复:重置buffer的position和limit
                buffer.clear();
                fcCR.read(buffer);
                s = new String(data);
                array = s.split(delimiter);
                if (array[0].equals(EMPTY_ACCT)) {
                    System.out.println("The account #" + accountNum + " does not exist.");
                } else {
                    System.out.println("Account #: " + array[0] + " Last Name: " + array[1] + " Account Balance: $" + array[2]);
                }
                System.out.println("Enter the next account number or 9999 to quit >> ");
                findAccount = input.nextLine();
                System.out.println(findAccount);
            }
            fcCR.close();
        } catch (Exception e) {
            System.out.println("Message: " + e);
        }
    }
}

输出示例

Enter account to display >>
1001
1001
24024
Account #: 1001 Last Name: Smith Account Balance: $34000.00

Enter the next account number or 9999 to quit >>
1203
1203
28872
Account #: 1001 Last Name: Smith Account Balance: $34000.00

Enter the next account number or 9999 to quit >>

问题原因及修复

问题出在ByteBuffer的状态没有重置:

  • 首次调用fcCR.read(buffer)后,buffer的position会移动到数组末尾,limit也等于数组容量。
  • 后续再调用read时,buffer的position已经等于limit,不会再读取新数据到data数组,所以始终用的是第一次读取的内容。

修复方法:每次读取文件前,调用buffer.clear(),该方法会将buffer的position重置为0,limit设置为容量,确保下次read操作能重新填充data数组。

内容的提问来源于stack exchange,提问作者HavannaMaker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 01:45:35