Rof*_*koc 3 java string bufferedreader bukkit
现在我正在为一个游戏编写一个小的聊天历史插件,其中我想保存所有消息以及它在文件中发送的时间,这是完美的,但是在阅读它时我遇到了一些问题.
变量:
history = new HashMap<Date, String>();
这是我加载消息的方式:
public static void load(){
File f = new File(config.getString("file"));
if (!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try{
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine())!=null){
String date = line.split(" ")[0];
for (int i = 0; i < line.split(" ").length; i++){
System.out.print(i+"="+line.split(" ")[i]+" ");
}
if (date.split(",")[0].split(".").length == 0) continue;
line = line.replaceAll(date+" ", "");
history.put(fromString(date), line);
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在文件中写的:
09.01.17,18:45:26 §6[RoflFrankoc] §cHi
09.01.17,18:45:30 §6[RoflFrankoc] §cHello world
Run Code Online (Sandbox Code Playgroud)
现在我的问题:该行if (date.split(",")[0].split(".").length == 0) continue;正在停止添加行history.没有它我得到一个ArrayOutOfBoundsError:0.这些行
for (int i = 0; i < line.split(" ").length; i++){
System.out.print(i+"="+line.split(" ")[i]+" ");
}
Run Code Online (Sandbox Code Playgroud)
我正在检查它是否正确读取,是的,输出:
0=09.01.17,18:45:30
1=§6[RoflFrankoc]
2=§cHello
3=world
0=09.01.17,18:45:26
1=§6[RoflFrankoc]
2=§cHi
Run Code Online (Sandbox Code Playgroud)
(§c和§6是API中的颜色代码,我正在使用SpigotAPI for Minecraft)
该字符.是一个特殊字符,如果您想String使用点作为分隔符进行拆分,则需要使用2个反斜杠将其转义为下一个:
if (date.split(",")[0].split("\\.").length == 0) continue;
Run Code Online (Sandbox Code Playgroud)
实际上请记住,该方法split(String regex)需要将正则表达式作为参数.