首页 > 解决方案 > 如何从 csv 文件读取数据,将其存储到内存,然后将内存写回 csv 文件(同时删除读取的最后一行)?

问题描述

我的代码使用缓冲读取器读取包含值列表的文件,并且我的 FileWriter 将另一个玩家添加到列表中。每当我运行我的代码时,都会重复添加新玩家。如何将数据读入内存,然后在不添加最后一行的情况下将内存写回文件?

输入:

package week3assignment;

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

public class Jaret_Dalrymple_GamesReport {

    public static void main(String[] args) throws IOException {
        File gameScores = new File("C:\\Users\\jaret\\OneDrive\\Desktop\\GameScores.csv");
        readWrite(gameScores);
        addGamer(gameScores, "Jimmy", "189", "190", "197", "199", "198", "193", "199", "199", "188", "196");
        readWrite2(gameScores);
    }

    private static void readWrite(File gameScores) throws IOException {

        if (gameScores.exists()) {
            BufferedReader br = null;
            String line = "";
            String csvSplitBy = ",";
            int gamerCount = 0;
            int topScore = 0;
            String topScorer = "";
            br = new BufferedReader(new FileReader(gameScores));
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Games Report");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Gamer   1   2   3   4   5   6   7");
            System.out.println("    8   9   10  Total");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");

            while ((line = br.readLine()) != null) {
                String list[] = new String[10];
                list = line.split(csvSplitBy);
                int sum = 0;

                for (String element : list) {
                    try {
                        Integer num = Integer.parseInt(element);
                        sum += num;
                    } catch (NumberFormatException nfe) {
                        System.out.print(list[0] + "\t" + list[1] + (list[1].length() > 10 ? "\t" : "\t") + list[2]
                                + (list[2].length() > 10 ? "\t" : "\t") + list[3]
                                + (list[3].length() > 10 ? "\t" : "\t") + list[4]
                                + (list[4].length() > 10 ? "\t" : "\t") + list[5]
                                + (list[5].length() > 10 ? "\t" : "\t") + list[6]
                                + (list[6].length() > 10 ? "\t" : "\t") + list[7]
                                + (list[7].length() > 10 ? "\n\t" : "\n\t") + list[8]
                                + (list[8].length() > 10 ? "\t" : "\t") + list[9]
                                + (list[9].length() > 10 ? "\t" : "\t") + list[10] + "\t");
                    }
                }
                if (sum > topScore) {
                    topScore = sum;
                    topScorer = list[0];
                }

                System.out.println(sum);
                gamerCount++;
            }

            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("# of Gamers:    " + gamerCount);
            System.out.println("Top Gamer:  " + topScorer);
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            br.close();
        }
    }

    private static void addGamer(File gameScores, String first, String second, String third, String fourth,
            String fifth, String sixth, String seventh, String eighth, String ninth, String tenth, String eleventh)
            throws IOException {
        FileWriter newGamer = new FileWriter(gameScores, true);
        newGamer.write("\n" + first + "," + second + "," + third + "," + fourth + "," + fifth + "," + sixth + "," + 
        seventh + "," + eighth + "," + ninth + "," + tenth + "," + eleventh);
        newGamer.close();
        System.out.println("Gamer Jimmy record added.");;
    }

    private static void readWrite2(File gameScores) throws IOException {

        if (gameScores.exists()) {
            BufferedReader br = null;
            String line = "";
            String csvSplitBy = ",";
            int gamerCount = 0;
            int topScore = 0;
            String topScorer = "";
            FileWriter fw = new FileWriter(gameScores, true);
            br = new BufferedReader(new FileReader(gameScores));
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Games Report");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Gamer   1   2   3   4   5   6   7");
            System.out.println("    8   9   10  Total");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");

            while ((line = br.readLine()) != null) {
                String list[] = new String[10];
                list = line.split(csvSplitBy);
                int sum = 0;

                for (String element : list) {
                    try {
                        Integer num = Integer.parseInt(element);
                        sum += num;
                    } catch (NumberFormatException nfe) {
                        System.out.print(list[0] + "\t" + list[1] + (list[1].length() > 10 ? "\t" : "\t") + list[2]
                                + (list[2].length() > 10 ? "\t" : "\t") + list[3]
                                + (list[3].length() > 10 ? "\t" : "\t") + list[4]
                                + (list[4].length() > 10 ? "\t" : "\t") + list[5]
                                + (list[5].length() > 10 ? "\t" : "\t") + list[6]
                                + (list[6].length() > 10 ? "\t" : "\t") + list[7]
                                + (list[7].length() > 10 ? "\n\t" : "\n\t") + list[8]
                                + (list[8].length() > 10 ? "\t" : "\t") + list[9]
                                + (list[9].length() > 10 ? "\t" : "\t") + list[10] + "\t");
                    }
                }
                if (sum > topScore) {
                    topScore = sum;
                    topScorer = list[0];
                }

                System.out.println(sum);
                gamerCount++;
            }

            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("# of Gamers:    " + gamerCount);
            System.out.println("Top Gamer:  " + topScorer);
            
            br.close();
        }
    }
}

输出:

----------------------------------------------------------
-------------------------------------
Games Report
----------------------------------------------------------
-------------------------------------
Gamer   1   2   3   4   5   6   7
    8   9   10  Total
----------------------------------------------------------
-------------------------------------
Bob 167 123 159 102 102 189 183
    173 197 148 1543
Sally   189 130 138 113 159 116 134
    196 150 144 1469
Mario   104 106 120 188 143 189 149
    174 163 100 1436
Lev 152 159 195 140 154 176 107
    128 166 181 1558
Carden  158 200 175 114 117 150 176
    181 131 132 1534
Adelie  175 199 122 104 198 182 175
    153 120 165 1593
Lada    161 108 102 193 151 197 115
    137 126 186 1476
Xavier  178 171 147 113 107 129 128
    189 165 195 1522
Raffi   176 144 151 124 149 112 158
    159 119 177 1469
Chang   135 144 177 153 143 125 145
    140 117 158 1437
Mich    156 105 178 137 165 180 128
    115 139 157 1460
Mason   162 185 108 106 113 135 139
    135 197 160 1440
Cora    186 115 106 126 135 108 157
    156 187 120 1396
Sergio  117 105 115 116 193 200 176
    134 122 153 1431
Jonas   132 163 196 101 134 159 131
    104 135 168 1423
Jimmy   189 190 197 199 198 193 199
    199 188 196 1948
----------------------------------------------------------
-------------------------------------
# of Gamers:    16
Top Gamer:  Jimmy
----------------------------------------------------------
-------------------------------------
Gamer Jimmy record added.
----------------------------------------------------------
-------------------------------------
Games Report
----------------------------------------------------------
-------------------------------------
Gamer   1   2   3   4   5   6   7
    8   9   10  Total
----------------------------------------------------------
-------------------------------------
Bob 167 123 159 102 102 189 183
    173 197 148 1543
Sally   189 130 138 113 159 116 134
    196 150 144 1469
Mario   104 106 120 188 143 189 149
    174 163 100 1436
Lev 152 159 195 140 154 176 107
    128 166 181 1558
Carden  158 200 175 114 117 150 176
    181 131 132 1534
Adelie  175 199 122 104 198 182 175
    153 120 165 1593
Lada    161 108 102 193 151 197 115
    137 126 186 1476
Xavier  178 171 147 113 107 129 128
    189 165 195 1522
Raffi   176 144 151 124 149 112 158
    159 119 177 1469
Chang   135 144 177 153 143 125 145
    140 117 158 1437
Mich    156 105 178 137 165 180 128
    115 139 157 1460
Mason   162 185 108 106 113 135 139
    135 197 160 1440
Cora    186 115 106 126 135 108 157
    156 187 120 1396
Sergio  117 105 115 116 193 200 176
    134 122 153 1431
Jonas   132 163 196 101 134 159 131
    104 135 168 1423
Jimmy   189 190 197 199 198 193 199
    199 188 196 1948
Jimmy   189 190 197 199 198 193 199
    199 188 196 1948
----------------------------------------------------------
-------------------------------------
# of Gamers:    17
Top Gamer:  Jimmy

标签: javacsvbufferedreaderfilereaderprintwriter

解决方案


当你有这个条件的while循环时: line = br.readLine() != null 你可以把它改成这样:

line = br.readLine();
while(line != null){
   your code here...
} // end loop

您可以创建一个临时变量来存储您在循环中使用的行,如果代码最终退出循环,请获取该行并将其从 csv 中删除。

PS:对不起,如果我没有像我想要的那样解释自己,就从这个开始。祝你好运!


推荐阅读