JAVA替换文本文件中的行或字符串不起作用

时间:2016-01-13 15:43:32

标签: java file bufferedwriter

所以我创建了两个创建文件的方法(createFile();如果没有设置,则创建一个用空高分填充的文本文件。

public class HighscoreList {
static String highscore = null;
static PuzzleModel theModel;
static File file = null;
public static int nom;
public static int tu;
public static int nor;
public static String search = " ";
static String replace = "2";
static String numberOfRows = null;
static String timeUsed = " ";
static String numberOfMoves = " ";

public static void main(String[] args) {
    createFile();
    isEmptySetEmptyHighscore();
    // checkScore(0);
    getHighscore(0);
}

public static void createFile() {
    file = new File("C:\\Users\\Thomas\\Eclipse Workspace\\15Puzzle\\15Puzzle\\src\\FifteenPuzzle\\ScoreBoard.txt");
    System.out.println("Created file " + file.getName());
    if (!file.exists()) {
        System.out.println("File didn't exist creating new file");
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public static void isEmptySetEmptyHighscore() {
    try {
        BufferedReader br = new BufferedReader(new FileReader(
                "C:\\Users\\Thomas\\Eclipse Workspace\\15Puzzle\\15Puzzle\\src\\FifteenPuzzle\\ScoreBoard.txt"));
        if (br.readLine() == null) {
            setEmptyHighscoreFile();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void setEmptyHighscoreFile() {
    try {
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        System.out.println("File is empty, fills with empty fields");
        for (int i = 3; i < 101; i++) {
            bw.write(i + ":" + numberOfMoves + ":" + timeUsed+"\n");
        }
        bw.close();
        System.out.println("Done");

    } catch (IOException e) {
        e.printStackTrace();
    }
}

我有一个getHighscore(),它使用move和timeUsed读取两个空的“”字段。它目前能够读取这个,但我不能写入文本文件中的那些空格并用我想要的实际数字替换它们。 编辑:使用replace命令,它只是将它添加到文件的底部。

我的代码是否有问题会重新删除我尝试替换的文本或如何执行此操作? 我试过这样的事情:

public static void writeToFile(int rows) {
    try {
        FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
        BufferedWriter bw = new BufferedWriter(fw);
        BufferedReader br = new BufferedReader(new FileReader(
                "C:\\Users\\Thomas\\Eclipse Workspace\\15Puzzle\\15Puzzle\\src\\FifteenPuzzle\\ScoreBoard.txt"));

        if(br.readLine().split(":")[0].equals(Integer.toString(rows+1))){
            bw.write(br.readLine().replaceFirst(rows+2+": : ", "yes"));
            System.out.println(" lel");
        }
        bw.close();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

你试试这个吗?

 String line = br.readLine();
 if(line.split(":")[0].equals(Integer.toString(rows+1))){
        bw.write(line.replaceFirst(rows+2+": : ", "yes"));
        System.out.println(" lel");
 }