Example usage for java.net URL openStream

List of usage examples for java.net URL openStream

Introduction

In this page you can find the example usage for java.net URL openStream.

Prototype

public final InputStream openStream() throws java.io.IOException 

Source Link

Document

Opens a connection to this URL and returns an InputStream for reading from that connection.

Usage

From source file:GetURL.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    InputStream urlstream = url.openStream();
    byte[] buffer = new byte[0];
    byte[] chunk = new byte[4096];
    int count;/*from   www .j a va 2s .c  o m*/

    while ((count = urlstream.read(chunk)) >= 0) {
        byte[] t = new byte[buffer.length + count];
        System.arraycopy(buffer, 0, t, 0, buffer.length);
        System.arraycopy(chunk, 0, t, buffer.length, count);
        buffer = t;
    }

    String filename = (url.getFile()).replace('/', File.separatorChar);
    File f1 = new File(filename);
    filename = f1.getName();
    FileOutputStream f = null;
    f = new FileOutputStream(filename);
    f.write(buffer);
    f.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ParserGetter kit = new ParserGetter();
    HTMLEditorKit.Parser parser = kit.getParser();
    HTMLEditorKit.ParserCallback callback = new ReportAttributes();

    URL u = new URL("http://www.java2s.com");
    InputStream in = u.openStream();
    InputStreamReader r = new InputStreamReader(in);
    parser.parse(r, callback, false);//from   ww w .ja  va2 s .c om
}

From source file:StAXTest.java

public static void main(String[] args) throws Exception {
    String urlString;//from  ww w.j av a 2 s .  c o  m
    if (args.length == 0) {
        urlString = "http://www.w3c.org";
        System.out.println("Using " + urlString);
    } else
        urlString = args[0];
    URL url = new URL(urlString);
    InputStream in = url.openStream();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.getLocalName().equals("a")) {
                String href = parser.getAttributeValue(null, "href");
                if (href != null)
                    System.out.println(href);
            }
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://localhost:1776");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;//  ww w . j a v a  2 s  . co m
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    in.close();
}

From source file:lycos.java

public static void main(String[] args) {

    try {/* w w w .j  ava 2s.c o m*/
        String thisLine;
        URL u = new URL("http://www.google.com");
        DataInputStream theHTML = new DataInputStream(u.openStream());
        while ((thisLine = theHTML.readLine()) != null) {
            System.out.println(thisLine);
        } // while loop ends here
    } catch (MalformedURLException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    }

}

From source file:Main.java

public static void main(String[] a) throws Exception {
    Properties p = new Properties();
    URL url = ClassLoader.getSystemResource("/com/java2s/config/system.props");
    if (url != null)
        p.load(url.openStream());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    Image chunk = readFragment(url.openStream(), new Rectangle(15, 15, 30, 25));
    JOptionPane.showMessageDialog(null, new ImageIcon(chunk), "Duke", JOptionPane.INFORMATION_MESSAGE);

}

From source file:com.github.tsouza.promises.example.BlockingIOPromise.java

public static void main(String[] args) throws Exception {

    Promises.defer((Resolver<String> resolver) -> {
        URL url = new URL("http://api.ipify.org");

        try (InputStream stream = url.openStream()) {
            resolver.resolve(IOUtils.toString(stream));
        }//w ww  . ja  va2s.c  o m

    }, ThreadProfile.IO).done(System.out::println);

    Thread.sleep(5000);
}

From source file:MainClass.java

public static void main(String[] args) {
    ParserGetter kit = new ParserGetter();
    HTMLEditorKit.Parser parser = kit.getParser();
    HTMLEditorKit.ParserCallback callback = new ReportAttributes();

    try {/*from  w w  w.  j  a v a2  s .  c  om*/
        URL u = new URL("http://www.java2s.com");
        InputStream in = u.openStream();
        InputStreamReader r = new InputStreamReader(in);
        parser.parse(r, callback, false);
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:URLGet.java

public static void main(String[] args) {
    BufferedReader in = null;/*from  ww w  . ja v a2 s.  c  o m*/
    if (args.length == 1) {
        try {
            URL url = new URL(args[0]);
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = null;
            while ((line = in.readLine()) != null)
                System.out.println(line);
        } catch (MalformedURLException ex) {
            System.err.println(ex);
        } catch (FileNotFoundException ex) {
            System.err.println("Failed to open stream to URL: " + ex);
        } catch (IOException ex) {
            System.err.println("Error reading URL content: " + ex);
        }
        if (in != null)
            try {
                in.close();
            } catch (IOException ex) {
            }
    } else
        System.err.println("Usage: URLGet URL");
}