Advertisement
Guest User

Main.java

a guest
May 29th, 2010
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package jcrawler;
  2.  
  3. import java.io.*;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6.  
  7. /**
  8.  * @author Jan Kuboschek
  9.  *
  10.  * Driver program that uses the JCrawler class to demonstrate its use.
  11.  *
  12.  * Creates JCrawler object with explicit constructor and two default JCrawler objects.
  13.  * doAction() has been overriden to store the content of the webpage in a local file.
  14.  * The file is created if it doesn't yet exist.
  15.  */
  16.  
  17. public class Main
  18. {
  19.     private static int m;
  20.  
  21.     public static void main(String[] args)
  22.     {
  23.         m=0;
  24.         new JCrawler("http://stpaulcreative.com") {
  25.  
  26.             @Override
  27.             void doAction(String URL, String content) {
  28.                 System.out.print("thread 1 ");
  29.                 writeToTextFile(m+".txt", content);
  30.                 m++;
  31.             }
  32.         };
  33.  
  34.         for (int i=0;i<2;i++)
  35.         {
  36.             final int n=i;
  37.             new JCrawler() {
  38.  
  39.             @Override
  40.             void doAction(String URL, String content) {
  41.                 System.out.print("thread "+(n+2)+" ");
  42.                 writeToTextFile(m+".txt", content);
  43.                 m++;
  44.             }
  45.         };
  46.         }
  47.     }
  48.  
  49.     /**************************************************
  50.      * Writes string to a specified file. If file does not exist, create new file.
  51.      **************************************************/
  52.     public static void writeToTextFile(String fileName, String toWrite)
  53.     {
  54.         try {
  55.             File file = new File(fileName);
  56.             if (file.exists() == false) {
  57.                 file.createNewFile();
  58.             }
  59.             Writer output = new BufferedWriter(new FileWriter(fileName, true));
  60.             output.write(toWrite + "\r\n");
  61.             output.close();
  62.         } catch (IOException ex) {
  63.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement