如何从文本中读取行?看看我的代码:如何从txt读取行?
public static String getTemplateFromFile() {
String name = null;
try {
BufferedReader reader = new BufferedReader(new
FileReader(
"http://localhost:8080/blog/resources/cache/templateName.txt"));
name = reader.readLine();
//name="TEST";
//NULL anyway
reader.close();
}
catch (Exception e) {
}
return name;
}
另外我有secnod版本,但我的服务器冻结。
public static String getTemplateFromFile() {
String name = null;
/*
try {
URL url = new URL("http://localhost:8080/blog/resources/cache/templateName.txt");
Scanner s = new Scanner(url.openStream());
name=s.nextLine();
s.close();
}
catch(IOException ex) {
ex.printStackTrace();
}*/
return name;
}
我认为它不能关闭连接或什么。 即使我在尝试构建中说name="TEST";
,它也会返回NULL。
这可能会引发异常。不要像那样默默地消费例外。 – arshajii
可能引发异常。在'catch'块中加入一些东西,比如'e.printStackException()'看看是否是这种情况。 –
嗯。真。如果我尝试这个'catch(Exception e){ } \t \t \t \t \t \t name =“TEST”; \t \t}'它会返回“测试”。 –