【编程技巧】JAVA读取url地址中的文本内容

应用场景:最常见的是有自已的网站,在注册广告联盟的时候。都需要下载一个文本文件来验证网站的有效性。例如淘宝、京东等都有这一过程


实现代码:
//读url地址的内容
        public void readTxt(){
                try {
                        URL url=new URL("http://www.oumeidy.com/demo.txt");
                        URLConnection curl=url.openConnection();
                        DataInput dins=new DataInput(curl.getInputStream());
                        String reads;
                        while((reads=dins.readLine())!=null){
                                System.out.println("txt 内容:     "+reads);
                        }
                        System.out.println("已显示-------------------------------------------");
                        //dins.close();
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

posted @ 2013-07-31 15:27  ejllen  阅读(853)  评论(0编辑  收藏  举报