我正在嘗試編寫一個用於抽搐的聊天機器人,並且我正在尋找將所有對象曾經訪問過頻道的觀衆以及我爲每個用戶存儲的一些數據。這些包括他們的用戶名,級別,他們擁有多少點等等。到目前爲止,我所使用的是使用String.format()
爲文件創建一個字符串,然後我們將其寫入一個文件,其中包含bufferedWritter
。我希望能夠在維護或添加新功能之後每次重新啓動機器人時都能看到瀏覽器列表。我只是不確定我是否以最有效的方式進行操作,而且我不知道如何在不運行for循環來檢查分號位置並使用子字符串填充數據的情況下閱讀它。如何將對象的數據寫入文件,然後在讀取文件時將其解析回新對象
public abstract class Viewer {
private String username;
private int level;
private int exp;
private int points;
private int intermediatePoints;
private boolean isMod;
private boolean isPreviousWinner;
private boolean enteredRaffle;
public Viewer(String username){
this.username = username;
this.level = 0;
this.exp = 0;
this.points = 0;
this.intermediatePoints = 0;
this.isMod = false;
this.isPreviousWinner = false;
this.enteredRaffle = false;
}
public Viewer(String username, int level, int exp, int points, int intermediatePoints,
boolean isMod, boolean isPreviousWinner, boolean enteredRaffle){
this.username = username;
this.level = level;
this.exp = exp;
this.points = points;
this.intermediatePoints = intermediatePoints;
this.isMod = isMod;
this.isPreviousWinner = isPreviousWinner;
this.enteredRaffle = enteredRaffle;
}
public String setViewerString(){
String viewerDataString = String.format("%s;%d;%d;%d;%d;%b;%b;%b;",
username, level, exp, points, intermediatePoints, isMod, isPreviousWinner, enteredRaffle);
return viewerDataString;
}
public void parseViewerDataString(){
}
}
對不起,如果我太具體了,我從來沒有問過這裏的問題,我不知道有多少信息太多。
請 '標籤' 的語言 – Michael
GSON在這裏似乎有用。可以像讀取4行代碼一樣讀取和寫入JSON對象 –