import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
public class urlio
{
public static void main(String[] args) throws Exception
{
//通过URL定位到一个网址
URL url = new URL("http://www.scu.edu.cn/");
/*可以通过URL得到一个URLConnection,然后通过这个Connection打开一个inputstream
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
*/
//也可以直接通过URL openStream
InputStream is = url.openStream();
OutputStream os = new FileOutputStream("D:\\scu.txt");
byte[] array = new byte[2048];
int length = 0;
while(-1 != (length = is.read(array, 0, array.length)))
{
os.write(array, 0, length);
}
os.close();
is.close();
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class buffer
{
public static void main(String[] args) throws Exception
{
URL url = new URL("http://www.google.com");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
while(null != (line = br.readLine()))
{
System.out.println(line);
}
br.close();
}
}