簡體   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