gpt4 book ai didi

java - 如何从另一个 .java 文件运行函数

转载 作者:行者123 更新时间:2023-12-04 04:45:59 24 4
gpt4 key购买 nike

所以我试图制作一个程序,你输入一个 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 中的函数?

最佳答案

这应该这样做:

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

关于java - 如何从另一个 .java 文件运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18173505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com