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

如何基于行索引与词索引读取文本文件指定内容并写入Java ArrayList

解决方案

要实现仅将指定坐标(lineIndex, wordIndex)的单词加入fileWords列表,你需要在遍历文件时跟踪当前行号和单词索引,仅当两者匹配目标值时才添加单词。以下是修改后的代码及说明:

修改后的完整代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class WordPicker {
    public static void main(String[] args)  {
        Scanner scan = new Scanner(System.in);
        ArrayList<String> fileWords = new ArrayList<>();
        String filePath = "your_file_path_here"; // 替换为实际文件路径
       
        // 可改为从控制台输入目标索引,示例用固定值演示
        int lineIndex = 1; // 目标行索引(从0开始计数)
        int wordIndex = 2; // 目标单词索引(从0开始计数)
        
        addWordsToArray(fileWords, filePath, lineIndex, wordIndex);
        
        // 输出验证结果
        if (!fileWords.isEmpty()) {
            System.out.println("匹配到的单词:" + fileWords.get(0));
        } else {
            System.out.println("未找到对应坐标的单词");
        }
    }

    public static void addWordsToArray(ArrayList<String> fileWords, String filePath, int lineIndex, int wordIndex) {
        Scanner lineScanner = null;
        try {
            lineScanner = new Scanner(new File(filePath));
            int currentLine = 0; // 跟踪当前遍历的行索引
            
            while (lineScanner.hasNextLine()) {
                String line = lineScanner.nextLine();
                // 仅处理目标行
                if (currentLine == lineIndex) {
                    Scanner wordScanner = new Scanner(line);
                    int currentWord = 0; // 跟踪当前单词的索引
                    
                    while (wordScanner.hasNext()) {
                        String word = wordScanner.next();
                        if (currentWord == wordIndex) {
                            fileWords.add(word);
                            // 找到目标后立即终止循环,提升效率
                            wordScanner.close();
                            lineScanner.close();
                            return;
                        }
                        currentWord++;
                    }
                    // 若该行单词数不足目标索引,直接退出
                    wordScanner.close();
                    break;
                }
                currentLine++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 确保扫描器资源被正确关闭
            if (lineScanner != null) {
                lineScanner.close();
            }
        }
    }
}

关键修改点

  • 行号与单词索引跟踪:新增currentLine和currentWord变量,分别记录当前遍历的行位置与单词位置,精准匹配目标坐标。
  • 提前终止逻辑:找到目标单词后立即关闭扫描器并返回,避免无意义的后续遍历。
  • 资源安全处理:在finally块中强制关闭扫描器,防止资源泄漏。
  • 边界情况兼容:如果目标行不存在、或该行单词数少于目标索引,程序不会报错,仅返回空列表。

示例验证

假设你的文件内容如下:

worda wordb wordc wordd
worde wordf wordg wordh
wordi wordj wordk wordl
wordm wordn wordo wordp

当设置lineIndex=1、wordIndex=2时,程序会将wordg加入fileWords列表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 08:20:27