我从url中的文本文件中抓取一行作为字符串,并且字符串返回正确的值。但是,如果我在读取字符串后返回null,则调用该字符串。缓冲读取器之后返回空字符串
我不知道发生了什么,并会感谢任何指导。
static protected String readURL() {
String u = "http://adamblanchard.co.uk/push.txt";
URL url;
InputStream is;
InputStreamReader isr;
BufferedReader r;
try {
System.out.println("Reading URL: " + u);
url = new URL(u);
is = url.openStream();
isr = new InputStreamReader(is);
r = new BufferedReader(isr);
do {
str = r.readLine();
if (str != null)
System.out.println(str); //returns correct string
} while (str != null);
} catch (MalformedURLException e) {
System.out.println("Invalid URL");
} catch (IOException e) {
System.out.println("Can not connect");
}
System.out.println(str); //str returns "null"
return str;
}