java笔记 一些和java网络及IO有关的代码

来源:互联网 发布:屏幕文字抓取软件 编辑:程序博客网 时间:2024/05/16 11:45
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 openStreamInputStream 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();}}


原创粉丝点击