Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jcrawler;
- import java.io.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * @author Jan Kuboschek
- *
- * Driver program that uses the JCrawler class to demonstrate its use.
- *
- * Creates JCrawler object with explicit constructor and two default JCrawler objects.
- * doAction() has been overriden to store the content of the webpage in a local file.
- * The file is created if it doesn't yet exist.
- */
- public class Main
- {
- private static int m;
- public static void main(String[] args)
- {
- m=0;
- new JCrawler("http://stpaulcreative.com") {
- @Override
- void doAction(String URL, String content) {
- System.out.print("thread 1 ");
- writeToTextFile(m+".txt", content);
- m++;
- }
- };
- for (int i=0;i<2;i++)
- {
- final int n=i;
- new JCrawler() {
- @Override
- void doAction(String URL, String content) {
- System.out.print("thread "+(n+2)+" ");
- writeToTextFile(m+".txt", content);
- m++;
- }
- };
- }
- }
- /**************************************************
- * Writes string to a specified file. If file does not exist, create new file.
- **************************************************/
- public static void writeToTextFile(String fileName, String toWrite)
- {
- try {
- File file = new File(fileName);
- if (file.exists() == false) {
- file.createNewFile();
- }
- Writer output = new BufferedWriter(new FileWriter(fileName, true));
- output.write(toWrite + "\r\n");
- output.close();
- } catch (IOException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement