因此,我试图制作一个程序,您输入一个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中的函数?
答案 0 :(得分:2)
这应该这样做:
public static void main(String[] args) {
new Main().getWebSite();
Searcher.main();
}
答案 1 :(得分:1)
在Searcher
类中创建Main
类的实例。
public static void main(String[] args) {
new Main().getWebSite();
Searcher search = new Searcher();
}
或简单地说,使用Searcher.main();
。
答案 2 :(得分: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);
答案 3 :(得分: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());
}
}
}