我想在java中实例化File,但该文件不驻留在我的本地磁盘上,它驻留在http服务器上。我试着做一些像
这样的事情File file = new File ("http://myserver.com/abc.txt");
但获得例外。怎么去呢?
答案 0 :(得分:2)
String data = "";
try {
// Create a URL for the desired page
URL url = new URL("http://myserver.com/abc.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
data += str + "\n"
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
System.out.println(data);
您无法创建文件,必须使用读者。
答案 1 :(得分:0)