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

Java(Windows环境)如何跳过TXT文件行并读取指定信息

Java读取文本文件:跳过首行与球员编号,提取姓名和位置

需求

在Windows环境下使用Java开发,需实现:

  • 跳过.txt文件的第一行
  • 跳过每行开头的球员编号
  • 仅读取球员的全名(名字+姓氏)和位置

现有代码

import java.util.Scanner;
import java.io.*;
import java.io.IOException;

public class AFLTeamMember {

    public static void main(String[] args) throws IOException {
        File fileContents = new File("WesternBulldogs.txt");
        Scanner inputFile = new Scanner(fileContents);
        String PlayerFirstName = inputFile.nextLine();
        String PlayerLastName = inputFile.nextLine();
        String PlayerPostition = inputFile.nextLine();
        System.out.println(PlayerFirstName + " " + PlayerLastName + "," + " " + PlayerPostition); 
    }
}

文本文件内容(WesternBulldogs.txt)

Western Bulldogs
Luke Beveridge, COACH
10, Easton Wood, FB
42, Alex Keath, FB
15, Taylor Duryea, FB
35, Caleb Daniel, HB
12, Zaine Cordy, HB
31, Bailey Dale, HB
6, Bailey Smith, C
21, Tom Liberatore, C
7, Lachie Hunter, C
19, Cody Weightman, HF
33, Aaron Naughton, HF
1, Adam Treloar, HF
39, Jason Johannisen, FF
44, Tim English, FF
29, Mitch Hannan, FF
8, Stefan Martin, FOL
11, Jack Macrae, FOL
4, Marcus Bontempelli, FOL, c
34, Bailey Williams, IC
5, Josh Dunkley, IC
37, Roarke Smith, IC
13, Josh Schache, IC

修改后的代码

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class AFLTeamMember {

    public static void main(String[] args) throws IOException {
        File teamFile = new File("WesternBulldogs.txt");
        Scanner inputFile = new Scanner(teamFile);
        
        // 跳过第一行的球队名称
        if (inputFile.hasNextLine()) {
            inputFile.nextLine();
        }
        
        // 循环读取剩余所有行
        while (inputFile.hasNextLine()) {
            String line = inputFile.nextLine().trim();
            // 跳过空行
            if (line.isEmpty()) {
                continue;
            }
            
            // 按逗号分割每行内容
            String[] lineParts = line.split(",");
            
            String fullName;
            String position;
            
            // 判断行类型:球员行(含编号)或教练行
            if (lineParts.length >= 3) {
                // 球员行:提取编号后的姓名和位置,去除首尾空格
                fullName = lineParts[1].trim();
                position = lineParts[2].trim();
            } else if (lineParts.length == 2) {
                // 教练行:直接提取姓名和职位
                fullName = lineParts[0].trim();
                position = lineParts[1].trim();
            } else {
                // 格式异常的行,跳过处理
                continue;
            }
            
            // 输出结果
            System.out.println(fullName + ", " + position);
        }
        
        // 关闭输入流,释放资源
        inputFile.close();
    }
}

代码说明

  1. 跳过首行:通过inputFile.nextLine()读取并丢弃第一行的球队名称
  2. 循环读取:用while(inputFile.hasNextLine())遍历所有剩余行,确保不会遗漏内容
  3. 内容分割与处理:按逗号分割每行,根据分割后的数组长度区分球员/教练行,提取对应内容并去除多余空格
  4. 异常行处理:跳过格式不符合的行,避免程序报错
  5. 资源释放:最后关闭Scanner输入流,符合IO操作规范

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:55:19