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

Java如何从气象站文本数据中提取指定字符串、int、double?

高效处理气象站数据的Java方案

针对你的需求,8700行数据量级不大,核心优化思路是一次性加载解析所有数据到内存缓存,后续查询直接从内存读取,避免反复IO操作;同时用更可靠的字符串分割替代按字符索引提取,提升解析效率。

实现步骤

1. 定义数据实体类

把每行数据的字段封装成实体类,方便后续存取:

class WeatherStationRecord {
    private String stationId;
    private int month;
    private int value1;
    private int value2;
    private int value3;

    // 构造方法:传入分割后的字段数组,直接解析所需字段
    public WeatherStationRecord(String[] parts) {
        this.stationId = parts[0];
        // 解析日期中的月份(格式如1/1/14)
        String[] dateParts = parts[4].split("/");
        this.month = Integer.parseInt(dateParts[0]);
        // 提取最后三个int值
        this.value1 = Integer.parseInt(parts[6]);
        this.value2 = Integer.parseInt(parts[7]);
        this.value3 = Integer.parseInt(parts[8]);
    }

    // Getter方法
    public String getStationId() { return stationId; }
    public int getMonth() { return month; }
    public int getValue1() { return value1; }
    public int getValue2() { return value2; }
    public int getValue3() { return value3; }
}

2. 高效加载并缓存数据

用BufferedReader快速读取文件,把所有数据存入HashMap(key为站点ID,value为对应实体),后续查询直接从Map获取:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WeatherDataHandler {
    public static Map<String, WeatherStationRecord> loadData(String filePath) throws IOException {
        Map<String, WeatherStationRecord> stationMap = new HashMap<>();
        // try-with-resources自动关闭流,避免资源泄漏
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty()) continue; // 跳过空行
                // 按任意空白字符分割(适配多个空格的情况)
                String[] parts = line.split("\\s+");
                if (parts.length < 9) { // 过滤格式错误的行
                    System.err.println("无效行:" + line);
                    continue;
                }
                WeatherStationRecord record = new WeatherStationRecord(parts);
                // 若站点有多行记录,直接覆盖,保留最后一行数据
                stationMap.put(record.getStationId(), record);
            }
        }
        return stationMap;
    }

    public static void main(String[] args) {
        try {
            String filePath = "你的气象数据文件路径.txt";
            Map<String, WeatherStationRecord> stationMap = loadData(filePath);

            // 示例1:获取4个站点的最后三个int值
            String[] targetStations1 = {"KE000063612", "STATION_ID_2", "STATION_ID_3", "STATION_ID_4"};
            for (String id : targetStations1) {
                WeatherStationRecord record = stationMap.get(id);
                if (record != null) {
                    System.out.printf("站点%s:%d, %d, %d%n",
                            id, record.getValue1(), record.getValue2(), record.getValue3());
                } else {
                    System.out.println("未找到站点:" + id);
                }
            }

            // 示例2:获取3个站点的月份信息
            String[] targetStations2 = {"KE000063612", "STATION_ID_5", "STATION_ID_6"};
            for (String id : targetStations2) {
                WeatherStationRecord record = stationMap.get(id);
                if (record != null) {
                    System.out.printf("站点%s的月份:%d%n", id, record.getMonth());
                } else {
                    System.out.println("未找到站点:" + id);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

为什么高效

  1. IO优化:BufferedReader通过缓冲机制减少磁盘IO次数,比逐字符读取快数倍;
  2. 内存缓存:一次性把所有数据加载到HashMap,后续查询是O(1)时间复杂度,避免反复读文件;
  3. 解析优化:用split("\\s+")替代按字符索引提取,既避免手动计算位置的错误,又利用了Java底层优化的字符串分割逻辑;
  4. 自动去重/保留最新记录:如果同一站点有多行数据,Map会自动覆盖旧记录,直接保留最后一行,符合你提取“最后”数据的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 15:20:46