我的班级中有两个静态块,它们从两个 URL 加载数据。我想使用单个静态块,并有效地读取数据。关于如何做到这一点的任何想法?
static {
URL urlA = null;
String data = "";
try {
url = new URL(urlA);
BufferedReader in = new BufferedReader(new InputStreamReader(urlA.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
data = data + inputLine;
}
} catch (MalformedURLException mue) {
e.printStackTrace();
} catch (IOException ioe) {
e.printStackTrace();
}
//Do stuff with the data
}
static {
URL urlB = null;
String data = "";
try {
url = new URL(urlB);
BufferedReader in = new BufferedReader(new InputStreamReader(urlB.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
data = data + inputLine;
}
} catch (MalformedURLException mue) {
e.printStackTrace();
} catch (IOException ioe) {
e.printStackTrace();
}
//Do stuff with the data
}