1 回答

TA貢獻(xiàn)1851條經(jīng)驗 獲得超3個贊
如果要保留循環(huán)代碼,則可以重構(gòu)代碼,使其看起來像這樣:
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();
}
}
它比您擁有的要好一些,更易于調(diào)試和閱讀。我建議您閱讀有關(guān)嘗試使用資源的信息。
根據(jù)經(jīng)驗,每次打開流時,都必須將其關(guān)閉。如果您自己不打開它,則不要關(guān)閉它。
添加回答
舉報