1 回答
TA贡献1851条经验 获得超3个赞
如果要保留循环代码,则可以重构代码,使其看起来像这样:
public void getData(int l, player tmp, level le) {
try (BufferedReader buff = new BufferedReader(new FileReader(new File(this.levelPath + l)))) {
String b;
while ((b = buff.readLine()) != null) {
if (b.contains("player")) {
String[] dataPlayer = b.split("-");
items it = new items(dataPlayer[1]); //because you know that you will have an array with only 2 elements
tmp.setInventory1(it);
}else if (b.contains("level")) {
String[] dataLevel = b.split("-");
items it = new items(dataLevel[1]); //because you know that you will have an array with only 2 elements
le.setSpecific(it);
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
它比您拥有的要好一些,更易于调试和阅读。我建议您阅读有关尝试使用资源的信息。
根据经验,每次打开流时,都必须将其关闭。如果您自己不打开它,则不要关闭它。
添加回答
举报