检查txt文件是否有文本,如果是,请不要再次写入相同的文本?

时间:2014-12-23 23:44:50

标签: file text duplicates

所以现在我在Minecraft中制作一个mod,它从服务器获取每个人的用户名并将其添加到txt文件,它可以工作,但问题是我不想复制名称当我再次使用该命令时。到目前为止,没有任何工作。如何检查txt是否已包含用户名,不要再次添加?谢谢。再次,我需要在将另一个名称写入列表之前,检查txt文件是否已包含名称,如果是,请不要添加它。

[code]
        for (int i = 0; i < minecraft.thePlayer.sendQueue.playerInfoList.size(); i++) {
            List playerList = minecraft.thePlayer.sendQueue.playerInfoList;
            GuiPlayerInfo playerInfo = (GuiPlayerInfo) playerList.get(i);
            String playerName = StringUtils.stripControlCodes(playerInfo.name);

            try {
                fileWriter = new FileWriter(GameDirectory() + "\\scraped.txt", true);
                bufferedReader = new BufferedReader(new FileReader(GameDirectory() + "\\scraped.txt"));
                lineNumberReader = new LineNumberReader(new FileReader(GameDirectory() + "\\scraped.txt"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            printWriter = new PrintWriter(fileWriter);

            try {
                fileWriter.write(playerName + "\r\n");
                lineNumberReader.skip(Long.MAX_VALUE);
            } catch (IOException e) {
                e.printStackTrace();
            }
            printWriter.flush();
        }
        addMessage("Scraped " + lineNumberReader.getLineNumber() + " usernames!");

    }

[/code]

我也试过这个,但是这个甚至不再写了。

[代码]

List playerList = minecraft.thePlayer.sendQueue.playerInfoList;

for (int i = 0; i < minecraft.thePlayer.sendQueue.playerInfoList.size(); i++) {
    GuiPlayerInfo playerInfo = (GuiPlayerInfo) playerList.get(i);
    String playerName = StringUtils.stripControlCodes(playerInfo.name);

    String lines;

    try {
        if ((lines = bufferedReader.readLine()) != null) {
            if (!lines.contains(playerName)) {
                bufferedWriter.write(playerName);
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

int linenumber = 0;

try {
    while (lineNumberReader.readLine() != null) {
        linenumber++;
    }
} catch (IOException e) {
    e.printStackTrace();
}

[/代码]

0 个答案:

没有答案