Posted By

lunu2840 on 04/15/19


Tagged


Versions (?)

urLReader


 / Published in: Java
 

Just takes a html file and counts the code in the file basically. I was just trying some stuff out i plan on using some of this code for a larger project but maybe some of you can use it

  1. import java.util.*;
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. public class urlReader {
  6. public static void main(String[] args) {
  7. operations e = new operations();
  8. e.openUrl();
  9. e.openFile();
  10. e.printFile();
  11. e.close();
  12. }
  13. }
  14.  
  15. class operations {
  16.  
  17. BufferedInputStream inputStream;
  18.  
  19. public void openUrl() {
  20. try {
  21. inputStream = new BufferedInputStream(new URL("http://bing.com").openStream());
  22. fileOS = new FileOutputStream("html.txt");
  23.  
  24. byte data[] = new byte[1024];
  25. int byteContent;
  26. while ((byteContent = inputStream.read(data, 0, 1024)) != -1) {
  27. fileOS.write(data, 0, byteContent);
  28. }
  29.  
  30. } catch (IOException e) {
  31. System.out.println("URL DOESNT EXIST");
  32. }
  33. }
  34. public void openFile() {
  35. try {
  36. x = new BufferedReader(new FileReader("html.txt"));
  37. } catch (Exception e) {System.out.println("Issue with file");
  38. }
  39. }
  40. public void printFile() {
  41. try {
  42. String text;
  43. for (int i = 0;(text = x.readLine()) != null;i++) {
  44. System.out.println(i);
  45. }
  46. } catch (Exception e) {}
  47. }
  48. public void close() {
  49. try {x.close();} catch (Exception e) {};
  50. }
  51. }

Report this snippet  

You need to login to post a comment.