所以现在我正在为一款名为Minecraft的游戏建立一个“朋友”系统。问题是,当你重新启动时,你没有朋友!无论如何,它都会创建一个新文件,而不是读取前一个文件。这是我的代码:
public static boolean friends = true;
public static List friend = new ArrayList();
public static void friendsList(){
if(friends){
try{
File file = new File("friends.txt");
BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter(file));
for(int i = 0; i < friend.size(); i++){
bufferedwriter.write((new StringBuilder()).append((String) friend.get(i)).append("\r\n").toString());
}
bufferedwriter.close();
}
catch(Exception exception){
System.err.print(exception.toString());
}
}
}
我不知道这是否非常重要,但是这里是你添加朋友的方式(par1Str是你发送聊天消息时输入的字符串):
if(par1Str.startsWith("&friendadd")){
Camb.friends = true;
String as0[] = par1Str.split(" ");
Camb.friend.add(as0[1]);
mc.thePlayer.addChatMessage("\2479[CAMB]\247e Added Friend.");
Camb.friendsList();
Camb.friends = false;
return;
}
如果文件已经存在,我将如何制作,它将从中加载而不是覆盖它?
由于
答案 0 :(得分:0)
您需要在某个时候阅读带有好友列表的文件。然后,您可以设置一个标志,以避免再次将其写出来。
答案 1 :(得分:0)
检查文件是否已存在非常容易。
File f = new File("friends.txt");
if(f.exists()){
System.out.println("File exists");
//Append to the end
} else{
System.out.println("File not found!");
}