我试图通过URL获取一些HTML并将其放入String中。这是我的努力:
public class
Bank {
public static void main(String[] args) throws IOException {
URL hh = new URL("https://m.hh.ru/");
BufferedReader in = new BufferedReader(
new InputStreamReader(hh.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
inputLine.concat(inputLine);//returns null. WTF?
System.out.println(inputLine);
}
in.close();
System.out.println(inputLine);
}
}
正如我所说,我想把它放到inputLine
,但它会返回NULL
。
怎么了?
答案 0 :(得分:3)
使用StringBuilder
进行字符串连接。循环应如下所示:
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine).append("\n");
}
System.out.println(sb);