我從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;
}