尝试从csv文件中获取信息

zysjyyx4  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(80)

所以我试图从csv文件中获取信息,然后过滤它,所以我得到六个随机行,其中的第17列等于我在其他活动中调用函数时作为参数的字符串位置,唯一不起作用的是我的CSV阅读器,在我的日志中它说可以找到文件ENOENT,我尝试了litteraly evrything,但无法找到解决方案,希望有人能帮助我解决这个问题

public class PlayerManager {
public static List<List<String>> getSixRandomPlayers(String position) {
    List<List<String>> players = readPlayersFromCSV();
    List<List<String>> filteredPlayers = filterPlayersByPosition(players, position);
    return selectRandomPlayers(filteredPlayers, 6);
}

private static List<List<String>> readPlayersFromCSV() {
    List<List<String>> players = new ArrayList<>();
    try (BufferedReader reader = new BufferedReader(new FileReader("raw/players.csv"))) {
        String line;

        while ((line = reader.readLine()) != null) {
            String[] playerData = line.split(",");
            List<String> player = new ArrayList<>();
            player.add(playerData[0]); // 1st column
            player.add(playerData[2]); // 3rd column
            player.add(playerData[6]); // 7nd column
            player.add(playerData[13]); // 14nd column
            player.add(playerData[7]); // 8th column
            player.add(playerData[16]); // 16nd column
            players.add(player);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return players;
}

private static List<List<String>> filterPlayersByPosition(List<List<String>> players, String position) {
    List<List<String>> filteredPlayers = new ArrayList<>();

    for (List<String> player : players) {
        String playerPosition = player.get(16);
        if (playerPosition.equals(position)) {
            filteredPlayers.add(player);
        }
    }

    return filteredPlayers;
}

private static List<List<String>> selectRandomPlayers(List<List<String>> players, int count) {
    Collections.shuffle(players);
    return players.subList(0, Math.min(count, players.size()));
}

}
这是日志2023-06-09 17:36:22.140 19626-19626/com.samsung.futtrivia W/System.err:java.io.FileNotFoundException:raw/players.csv:打开失败:ENOENT(没有这样的文件或目录)

nr9pn0ug

nr9pn0ug1#

文件可能不在程序所期望的位置。
尝试用下面的方法创建一个文件,这将给予你一个线索,把输入文件放在哪里。

private static void newFileLocation() {
        File test = new File("data.txt");

        try {
            test.createNewFile();
            System.out.println("File created at: " + test.getAbsolutePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

相关问题