2

所以我试图制作一个程序,你输入一个 Flash 游戏 URL 并下载 .swf 文件。此处显示:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


/**
 * Main.java
 *
 * 
 */
public class Main {

    /**
     * Reads a web page into a StringBuilder object
     * and prints it out to console along with the
     * size of the page.
     */
    public void getWebSite() {

        try {

            URL url = new URL("http://www.vivalagames.com");
            URLConnection urlc = url.openConnection();

            BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());

            StringBuilder builder = new StringBuilder();
            int byteRead;
            while ((byteRead = buffer.read()) != -1)
                builder.append((char) byteRead);

            buffer.close();

            Logger.log(builder.toString());
            System.out.println("The size of the web page is " + builder.length() + " bytes.");

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Starts the program
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().getWebSite();
    }
}

我已经到了下载网站 html 并将其放入名为 output.txt 的文件中的部分。现在我想做的是让它搜索那个文本文件,直到找到“.swf”这个词,搜索器代码是:

import java.io.*;
import java.util.Scanner;
import java.util.regex.MatchResult;

public class Sercher {
    public static void main() throws FileNotFoundException {
        Scanner s = new Scanner(new File("output.txt"));
        while (null != s.findWithinHorizon("(?i)\\b.swf\\b", 0)) {
            MatchResult mr = s.match();
            System.out.printf("Word found: %s at index %d to %d.%n", mr.group(),
                    mr.start(), mr.end());
        }


    }
}

现在如何让 main.java 代码运行 Searcher.java 中的函数?

4

4 回答 4

2

这应该这样做:

public static void main(String[] args) {
    new Main().getWebSite();
    Searcher.main();
}
于 2013-08-11T15:30:26.917 回答
1

Searcher在类中创建类的实例Main

public static void main(String[] args) {
   new Main().getWebSite();
   Searcher search = new Searcher();
}

或者简单地说,使用Searcher.main();.

于 2013-08-11T15:36:37.973 回答
0

首先,将下载的 HTML 存储在一个文件中以在之后重新读取该文件并不是一个好主意。你可以在内存中做任何事情。

从对象和方法的角度思考。这里基本上有两个对象:一个下载器和一个搜索器。而且您不希望程序有两种主要方法:只有一种。这个主要方法应该是这样的:

// create the object which downloads the HTML
Downloader downloader = new Downloader();

// Ask it to download, and store the result into a String variable
String downloadedHtml = downloader.download();

// create the object which can search into a String for .swf references
Searcher searcher = new Searcher();

// pass it the String to search into
searcher.searchSwfIn(downloadedHtml);
于 2013-08-11T15:35:25.500 回答
0

您需要将您的类放入包并将 Searcher 包导入您的主类。例子:

package foo.bar.package;    
import for.bar.package2.Searcher;
/* 
  Other import declarations
*/    
public class Main {    
/*
  Your code
*/     
    public static void main(String[] args) {
        new Main().getWebSite();
        new Searcher().search();    
    }    
}

package for.bar.package2;
/* 
  Import declarations
*/    
public class Searcher {
   public void search() throws FileNotFoundException {
        Scanner s = new Scanner(new File("output.txt"));
        while (null != s.findWithinHorizon("(?i)\\bjava\\b", 0)) {
            MatchResult mr = s.match();       

        System.out.printf("Word found: %s at index %d to %d.%n", mr.group(),
                mr.start(), mr.end());
    }

}

}

于 2013-08-11T15:37:09.920 回答