Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的BufferedReader不能读取文本文件?_Java_Bufferedreader - Fatal编程技术网

Java 为什么我的BufferedReader不能读取文本文件?

Java 为什么我的BufferedReader不能读取文本文件?,java,bufferedreader,Java,Bufferedreader,为什么我的BufferedReader不能工作?我试图使用BufferedReader读取文本文件,但每次运行程序时,它都会说我的行中有一个错误: BufferedReader br = new BufferedReader(new FileReader("playerinfo.txt")); 我还在学习,我不知道我的BufferedReader出了什么问题。我试图让它读取球员统计数据的文本文件,然后输出该文件。我制作了一个文本文件来读取,但每次尝试运行程序时,错误仍然会出现。我曾尝试查找与我

为什么我的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);
    }
}

包含main方法的类应声明为公共类

您也应该考虑使用泛型列表,因为您的实现现在产生警告。


使用列表防御=新建ArrayList

Java之于Javascript,犹如痛苦之于绘画,火腿之于汉普斯特。他们完全不同。强烈建议有抱负的程序员尝试学习他们试图编写代码的语言的名称。发布问题时,请适当标记。那么错误是什么?我的错误是:线程主java.io.FileNotFoundException:playerinfo.txt中的异常系统无法找到指定的文件,请输入文件的完整路径。