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