首页 / JAVA / java读入URL数据
java读入URL数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java读入URL数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1776字,纯文字阅读大概需要3分钟。
内容图文
![java读入URL数据](/upload/InfoBanner/zyjiaocheng/1116/8d71bd86e1574afeb85136b3b943f6b6.jpg)
转载:
URL类中的openStream()方法,可以读取一个URL对象所指定的资源,返回一个InputStream对象。 (1)file协议介绍 File协议主要用于访问本地计算机中的文件,就如同在Windows资源管理器中打开文件一样。 要使用File协议,基本的格式如下:file:///文件路径(或者是file://localhost,但不能是file://本机IP),比如要打开F盘flash文件夹中的1.swf文件,那么可以在资源管理器或IE地址栏中键入:file:///f:/flash/1.swf并回车。 (2)URL数据读入(读入本地数据) public class ReadFromURL { @SuppressWarnings("deprecation") public static void main(String[] args) { URL root=null; URL url=null; String readString; DataInputStream dis; try { root=new URL(file://localhost/d:/javaUrlTest/);//根地址,总的文件夹 url=new URL(root,args[0]);//文件夹中的某个文件对应的URL System.out.println("URL"+url); dis=new DataInputStream(url.openStream());//得到数据输入流 while((readString=dis.readLine())!=null) { System.out.println("readString:"+readString); } System.out.println("文件读完"); dis.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(IOException e) { System.out.println("IO异常:"+ e); } } } 运行代码,在控制台输入要读取的文件名。Run as——Run Configurations 因为DataInputStream类中的readline()方法在新的版本中已经不再使用,修改代码为 public class ReadFromURL { @SuppressWarnings("deprecation") public static void main(String[] args) { URL root=null; URL url=null; String readString; DataInputStream dis; BufferedReader br; try { root=new URL(file://localhost/d:/javaUrlTest/); url=new URL(root,args[0]); System.out.println("URL"+url); dis=new DataInputStream(url.openStream()); br=new BufferedReader(new InputStreamReader(url.openStream())); while((readString=br.readLine())!=null) { System.out.println("readString:"+readString); } System.out.println("文件读完"); dis.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(IOException e) { System.out.println("IO异常:"+ e); } } }
原文:http://www.cnblogs.com/FTDtt/p/4684449.html
内容总结
以上是互联网集市为您收集整理的java读入URL数据全部内容,希望文章能够帮你解决java读入URL数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。