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

Java读取txt文件失败求助:需实现均值、标准差、众数统计

Java读取本地TXT文件提示找不到的排查方案

问题描述

编写了读取TXT文件并计算均值、标准差、众数的Java代码,即使使用绝对路径,程序仍提示找不到已存在的目标文件。以下是原代码:

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

class Stats {

    public static void main(String[] args) {

        String fileName = "C:\\numbers.txt";
        int nums[] = new int[101], num;
        try {
            Scanner fileReader = new Scanner(new File(fileName));
            while (fileReader.hasNextInt()) {
                num = fileReader.nextInt();
                nums[num] += 1;
            }
            fileReader.close();
        } catch (FileNotFoundException e) {
            System.out.println("Error: could not find the input file.");
            return;
        }

        long total = 0;
        int count = 0;
        double average;

        //Find the average of the list of numbers.
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                total += i * nums[i];
                count += nums[i];
            }
        }
        average = total * 1.0 / count;
        System.out.println("Average: "+average);

        double squareSum = 0;
        //b. Determine the difference of each number from the average, and square
        // each difference.
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                //Sum all the differences.
                squareSum += nums[i] * (Math.pow(i - average, 2));
            }
        }
        //Divide this sum by (the number of values - 1).
        squareSum = squareSum / (count - 1);
        // d. Take the square root of the above division result from step
        double standarDeviation = Math.sqrt(squareSum);
        System.out.println("Standard Deviation: " +standarDeviation);

        // Find the mode now
        int maxNum = 0;
        for(int i=0; i<nums.length;i++){
            if(nums[i]> nums[maxNum]) maxNum = i;
        }
        System.out.println("Mode: "+ maxNum);

    }
}

预期输出:

Average: 49.91791791791792
Standard Deviation: 29.453867826299614
Mode: 10

排查与解决步骤

  1. 验证路径准确性

    • 确认文件确实位于C:\根目录,而非其他子文件夹(比如C:\Users\你的用户名\Documents)。
    • 检查文件名拼写,包括大小写(Windows系统默认不区分,但部分特殊场景可能有影响)。
  2. 检查文件后缀隐藏问题

    • Windows默认隐藏已知文件类型的后缀,可能实际文件名为numbers.txt.txt,但显示为numbers.txt。可通过「文件资源管理器」→「查看」→勾选「文件扩展名」来确认。
  3. 权限排查

    • C盘根目录可能受系统权限限制,Java程序无法读取。建议将文件移动到桌面等权限宽松的目录,修改路径为:
      String fileName = "C:\\Users\\你的用户名\\Desktop\\numbers.txt";
      

    替换你的用户名为实际系统用户名。

  4. 添加调试代码确认文件状态
    在创建Scanner前加入文件存在性判断,输出实际路径辅助排查:

    String fileName = "C:\\numbers.txt";
    File file = new File(fileName);
    // 新增调试代码
    if (!file.exists()) {
        System.out.println("文件不存在,实际路径:" + file.getAbsolutePath());
        return;
    }
    if (!file.canRead()) {
        System.out.println("无文件读取权限");
        return;
    }
    // 原读取代码
    try {
        Scanner fileReader = new Scanner(file);
        // ... 后续代码不变
    }
    
  5. 其他可能原因

    • 文件被其他程序占用(比如记事本打开未关闭),关闭占用程序后重试。
    • 杀毒软件或安全工具拦截了Java程序的文件访问,可临时关闭测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 14:10:45