Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.Reader;
- import java.net.MalformedURLException;
- import java.net.URL;
- /**
- * @author SungChul-Kim
- */
- public class ContentClassGetter {
- public static void main(String[] args) {
- try {
- URL u = new URL("http://www.naver.com");
- Class<?>[] types = new Class[3];
- types[0] = String.class;
- types[1] = Reader.class;
- types[2] = InputStream.class;
- Object o = u.getContent(types);
- if (o instanceof String) {
- System.out.println(o);
- } else if (o instanceof Reader) {
- int c;
- Reader r = (Reader) o;
- while ((c = r.read()) != -1) System.out.println((char) c);
- r.close();
- } else if (o instanceof InputStream) {
- int c;
- InputStream in = (InputStream) o;
- while((c = in.read()) != -1) System.out.write(c);
- in.close();
- } else {
- System.out.println("Error: unexpected tyupe " + o.getClass());
- }
- } catch (MalformedURLException ex) {
- System.err.println(ex);
- } catch (IOException ex) {
- System.err.println(ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement