当前位置: 技术问答>java相关
java 进阶级问题,有关网络的问题,请多多帮助
来源: 互联网 发布时间:2015-01-26
本文导语: 我想读取某个服务器上的txt文件里的内容,可以在IE浏览到,我用的方法是: url = new URL("/tech-qa-java/某文件的URL路径/index.html"); urlConnection = url.openConnection(); DataInputStream inStream =new DataInputStream(urlConnection.getInputStream()); String str = i...
我想读取某个服务器上的txt文件里的内容,可以在IE浏览到,我用的方法是:
url = new URL("/tech-qa-java/某文件的URL路径/index.html");
urlConnection = url.openConnection();
DataInputStream inStream =new DataInputStream(urlConnection.getInputStream());
String str = inStream.readUTF();
这样做,编译没问题,但是运行时,最后一行会出错,为什么读不到呢
该怎么做
url = new URL("/tech-qa-java/某文件的URL路径/index.html");
urlConnection = url.openConnection();
DataInputStream inStream =new DataInputStream(urlConnection.getInputStream());
String str = inStream.readUTF();
这样做,编译没问题,但是运行时,最后一行会出错,为什么读不到呢
该怎么做
|
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}