Java read-in URL data

Source: Internet
Author: User

Reproduced:

The OpenStream () method in the URL class, which can read a resource specified by a URL object, returns a InputStream object.   (1) File Protocol Introduction The file protocol is primarily used to access files on the local computer, just as you would open a file in Windows Explorer. To use the file protocol, the basic format is as follows: file:///file path (or file://localhost, but not file://native IP), For example, to open the 1.swf file in the Flash folder of the F drive, you can type: file:///f:/flash/1.swf and enter in the Explorer or IE Address bar. (2) URL data read in (read local data) public class readfromurl{@SuppressWarnings ("deprecation") public static void main (string[] args) {UR  L Root=null;  URL Url=null;  String readString;  DataInputStream dis; try {root=new URL (file://localhost/d:/javaUrlTest/),//root address, total folder url=new URL (root,args[0]),//URL System for a file in the folder.      Out.println ("URL" +url); Dis=new DataInputStream (Url.openstream ());//Get Data input stream while ((Readstring=dis.readline ())!=null) {System.out.println (   "ReadString:" +readstring);   } System.out.println ("file read");  Dis.close ();  } catch (Malformedurlexception e) {//TODO auto-generated catch block E.printstacktrace ();  }catch (IOException e) {System.out.println ("IO exception:" + E); }}} run the code and enter the name of the file to be read in the console. Run As--run CoNfigurations because the ReadLine () method in the DataInputStream class is no longer used in the new version, the code is changed to 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 ("file read");  Dis.close ();  } catch (Malformedurlexception e) {//TODO auto-generated catch block E.printstacktrace ();  }catch (IOException e) {System.out.println ("IO exception:" + E); } }}

  

Java read-in URL data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.