繁体   English   中英

用Java编写文件

[英]Writing a file in Java

美好的一天!

我有一个项目(游戏),明天早上需要提交。 但是我在高分写作时发现了一个错误。 我正在尝试创建一个文本文件,并以分数为基础按降序写入SCORE NAME。

例如:

SCORE   NAME           RANK
230     God
111     Galaxian     
10      Gorilla
5       Monkey
5       Monkey
5       Monkey

注意还有一个我的代码如下:

  public void addHighScore() throws IOException{
        boolean inserted=false;

        File fScores=new File("highscores.txt");
        fScores.createNewFile();

        BufferedReader brScores=new BufferedReader(new FileReader(fScores));
        ArrayList vScores=new ArrayList();
        String sScores=brScores.readLine();
        while (sScores!=null){
                if (Integer.parseInt(sScores.substring(0, 2).trim()) < score &&     inserted==false){
                        vScores.add(score+"\t"+player+"\t"+rank);
                        inserted=true;
                }
                vScores.add(sScores);
                sScores=brScores.readLine();
        }
        if (inserted==false){
                vScores.add(score+"\t"+player+"\t"+rank);
                inserted=true;
        }
        brScores.close();

        BufferedWriter bwScores=new BufferedWriter(new FileWriter(fScores));
        for (int i=0; i<vScores.size(); i++){
                bwScores.write((String)vScores.get(i), 0, ((String)vScores.get(i)).length());
                bwScores.newLine();
        }
        bwScores.flush();
        bwScores.close();
}

但是,如果我输入三个数字:60 Manny,则文件将如下所示:

   60      Manny
    230     God
    111     Galaxian     
    10      Gorilla
    5       Monkey
    5       Monkey
    5       Monkey

问题是它只能读取2个数字,因为我使用sScores.substring(0, 2).trim()) 我尝试将其更改为sScores.substring(0, 3).trim()) 但由于读取到字符部分而成为错误。 谁能帮助我修改代码,以便我最多读取4个数字? 非常感谢您的帮助。

您应该使用的是:

String[] parts = sScrores.trim().split("\\s+", 2);

然后,您将得到一个数组,其数字在索引0处,名称在索引1处。

int theNumber = Integer.parseInt(parts[0].trim();
String theName = parts[1].trim();

您可以像这样重新编写while循环:

String sScores=brScores.readLine().trim();
while (sScores!=null){
        String[] parts = sScrores.trim().split(" +");
        int theNumber = Integer.parseInt(parts[0].trim();
        if (theNumber < score &&     inserted==false){
                vScores.add(score+"\t"+player+"\t"+rank);
                inserted=true;
        }
        vScores.add(sScores);
        sScores=brScores.readLine();
}

就个人而言,我将添加一个新的HighScore类以帮助解析文件。

class HighScore {

    public final int score;
    public final String name;
    private HighScore(int scoreP, int nameP) {
        score = scoreP;
        name = nameP;
    }

    public String toString() {
        return score + "  " + name;
    }

    public static HighScore fromLine(String line) {
        String[] parts = line.split(" +");
        return new HighScore(Integer.parseInt(parts[0].trim()), parts[1].trim());
    }
}

每行的格式始终相同:一个整数,后跟一个制表符,然后是播放器名称。

只需在解析分数之前找到每行中制表符的索引,然后从0(含)到此索引(不包含)的子字符串。

玩家名称可以通过从选项卡索引+1(含)开始沿行的长度(不含)的子字符串获得。

如果上面提到的表是一个文件。

对于前两个分数,这会很好,但是对于5,它将开始读取字符。 可能是造成问题的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM