Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.Reader;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6.  
  7. /**
  8. * @author SungChul-Kim
  9. */
  10. public class ContentClassGetter {
  11.  
  12. public static void main(String[] args) {
  13. try {
  14. URL u = new URL("http://www.naver.com");
  15. Class<?>[] types = new Class[3];
  16. types[0] = String.class;
  17. types[1] = Reader.class;
  18. types[2] = InputStream.class;
  19. Object o = u.getContent(types);
  20.  
  21. if (o instanceof String) {
  22. System.out.println(o);
  23. } else if (o instanceof Reader) {
  24. int c;
  25. Reader r = (Reader) o;
  26. while ((c = r.read()) != -1) System.out.println((char) c);
  27. r.close();
  28. } else if (o instanceof InputStream) {
  29. int c;
  30. InputStream in = (InputStream) o;
  31. while((c = in.read()) != -1) System.out.write(c);
  32. in.close();
  33. } else {
  34. System.out.println("Error: unexpected tyupe " + o.getClass());
  35. }
  36. } catch (MalformedURLException ex) {
  37. System.err.println(ex);
  38. } catch (IOException ex) {
  39. System.err.println(ex);
  40. }
  41. }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement