代码是从文件中读取信息,使用该信息创建对象,然后将其添加到名为servers的ArrayList中。
try {
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)));
String line = "";
while ((line = br.readLine()) != null) {
String name = "";
String ip = "";
String port = "";
String checkFrequency = "";
int counter = 1;
boolean alert = true;
for (String value : line.split(",")){
if (counter == 1){
name = value;
}else if (counter == 2){
ip = value;
}else if (counter == 3){
port = value;
}else if (counter == 4){
checkFrequency = value;
}else if (counter == 5){
alert = Boolean.parseBoolean(value);
}
counter++;
}
MCServer server = new MCServer(name, ip, port, checkFrequency, alert);
servers.add(server);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
存储在文件中的示例行:
Name,199.99.99.99,80,60,true
是否有更好的方法来检索该信息,以便能够将其存储在正确的变量中,而无需使用带有计数器的循环,如上所示?
答案 0 :(得分:2)
怎么样:
try (BufferedReader br = new BufferedReader(
new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)))) {
String line = null; // start with null in case there is no line
while ((line = br.readLine()) != null) {
String[] tokens = line.split(",");
MCServer server =
new MCServer(tokens[0], tokens[1], tokens[2], tokens[3],
Boolean.parseBoolean(tokens[4]));
servers.add(server);
}
}
答案 1 :(得分:1)
你可以忽略计数器并直接使用令牌,但你至少应该确定有足够的令牌:
try
{
BufferedReader br = new BufferedReader(new InputStreamReader());
String line = null;
String[] tokens;
while ((line = br.readLine()) != null) {
tokens = line.split(",");
MCServer test = new MCServer(tokens[0],tokens[1],tokens[2],tokens[3],Boolean.valueOf(tokens[4]));
}
}
catch (IndexOutOfBoundsException e) { // <- be sure to catch this
// not enough elements in array
}
此外,您将字符串作为IP地址和端口传递,它们是字符串,但应检查它们是否可以转换,因此您可以Integer.valueOf(tokens[2])
例如只是为了引发NumberFormatException
。
答案 2 :(得分:0)
试试这个
try {
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)));
String line = "";
while ((line = br.readLine()) != null) {
String name = "";
String ip = "";
String port = "";
String checkFrequency = "";
int counter = 1;
boolean alert = true;
String s[] = line.split(",");
name = s[0];
ip = s[1];
port = s[2];
checkFrequency= s[3];
alert = s[4];
MCServer server = new MCServer(name, ip, port, checkFrequency, alert);
servers.add(server);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 3 :(得分:0)
我已经修改了你的代码:
try {
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)));
String line;
while ((line = br.readLine()) != null) {
String[] lineSplitted = line.split(",");
String name = lineSplitted[0];
String ip = lineSplitted[1];
String port = lineSplitted[2];
String checkFrequency = lineSplitted[3];
boolean alert = Boolean.parseBoolean(lineSplitted[4]);
MCServer server = new MCServer(name, ip, port, checkFrequency, alert);
servers.add(server);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
的变化:
初始化变量然后设置它们是多余的,如果没有事先设置它们就永远不会访问它们,所以如果你在第一个地方拆分你的线路,你可以立即用值初始化它们
循环
catch块可以折叠; IOException
涵盖FileNotFoundException