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();
    }
}

Web サイトの 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 をファイルに保存して、そのファイルをすぐに再読み込みするのはあまり良い考えではありません。すべてをメモリ内で行うことができます。

オブジェクトとメソッドの観点から考えてください。ここには基本的に、Downloader と Searcher の 2 つのオブジェクトがあります。また、プログラムに 2 つの主要なメソッドを含める必要はありません。1 つだけです。このメイン メソッドは次のようになります。

// 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 に答える