【问题标题】:Why won't my BufferedReader work at reading a text file?为什么我的 BufferedReader 不能读取文本文件?
【发布时间】:2018-09-18 04:46:59
【问题描述】:

为什么我的 BufferedReader 不能工作?我正在尝试使用 BufferedReader 读取文本文件,但每次运行程序时它都会说我的行中有错误:

BufferedReader br = new BufferedReader(new FileReader("playerinfo.txt"));

我还在学习,我不知道我的 BufferedReader 出了什么问题。我试图让它读取球员统计数据的文本文件,然后输出文件。我制作了一个要读取的文本文件,但每次尝试运行我的程序时仍然会出现错误。我试图查找与我类似的问题,但似乎有几种方法可以做一个 BufferedReader,我认为我的看起来应该可以工作。我在我的代码中找不到任何其他看起来错误的部分,但我还是比较新的。非常感谢您对此问题的任何帮助。

这是我尝试实现它的方式:

import java.util.*;
import java.io.*;

class Player { //Base class
    String name;
    float weight;
    int age;

    Player() {
    }

    ;

    Player(String name, float weight, int age) {
        this.name = name;
        this.weight = weight;
        this.age = age;
    }
}

class Defensive extends Player {
    int tackles, interceptions;
    float sacks;

    Defensive() {
        super();
    }

    Defensive(String name, float weight, int age, int tackles, int interceptions, float sacks) {
        super(name, weight, age);
        this.tackles = tackles;
        this.interceptions = interceptions;
        this.sacks = sacks;
    }

    public String toString() {
        return "name : " + name + ", weight : " + weight + ", age : " + age + ", tackles : " + tackles + ", sacks : " + sacks + ", interceptions : " + interceptions + "\n";
    }
}

class Football { //main class
    public Defensive readDefensive(BufferedReader br) throws IOException {
        String name = br.readLine();
        float weight = Float.parseFloat(br.readLine());
        int age = Integer.parseInt(br.readLine());
        int tackles = Integer.parseInt(br.readLine());
        float sacks = Float.parseFloat(br.readLine());
        int interceptions = Integer.parseInt(br.readLine());
        return (new Defensive(name, weight, age, tackles, interceptions, sacks));
    }

    public static void main(String[] args) throws IOException {
        Football football = new Football();
        BufferedReader br = new BufferedReader(new FileReader("playerinfo.txt"));
        String line = br.readLine();
        Defensive defensive = new Defensive();
        ArrayList al = new ArrayList();
        while (line != null) {
            if (line.equals("defensive")) {
                defensive = football.readDefensive(br);
                al.add(defensive);
            }
            line = br.readLine();
        }
        System.out.println(al);
    }
}

【问题讨论】:

  • Java 之于 Javascript 就像 Pain 之于绘画,或者 Ham 之于 Hampster。它们完全不同。强烈建议有抱负的程序员尝试学习他们尝试编写代码的语言的名称。当您发布问题时,请适当地标记它。
  • 那么错误是什么?
  • 我的错误是:
  • 线程“main”java.io.FileNotFoundException中的异常:playerinfo.txt(系统找不到指定的文件)
  • 尝试将完整路径放入文件。

标签: java bufferedreader


【解决方案1】:

包含 main 方法的类 Football 应声明为 public class

您还应该考虑使用泛型列表,因为您的实现现在会产生警告。 使用List<Defensive> defensives = new ArrayList<>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2017-03-20
    • 2019-04-12
    • 2022-08-14
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    相关资源
    最近更新 更多