我有一个 java 程序,它连接到在线资源,读取数据,然后解析特定的信息(它是查看某个页面的活动 reddit 帐户的数量)。
我想自动化这个过程以在给定的时间间隔重复它(我将时间间隔设置为 5 秒只是为了看看它是否有效)。然后程序将这个数字打印到一个文件中,每次都在不同的行上。我知道主循环正在迭代,因为我的output.txt
文件有几行,但它只在第一次迭代时找到并打印数字。
package redditreader3;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RedditReader3 {
public static void main(String[] args) throws IOException, InterruptedException
{
int i = 1;
String host = args[0]; // www.reddit.com
String resource = args[1]; // /r/toronto/about.json
final int HTTP_PORT = 80;
String command = "GET " + resource + " HTTP/1.1\n" + "Host:" + host
+ "\n\n";
/* This command requests reddit for the source code of the resource in args[1] at its host, args[0] to be printed through HTTP. */
Socket socket = new Socket(host, HTTP_PORT);
InputStream instream = socket.getInputStream();
Scanner in = new Scanner(instream);
OutputStream outstream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outstream);
File file = new File("output.txt");
FileOutputStream F_outstream = new FileOutputStream(file);
PrintStream F_printstream = new PrintStream(F_outstream);
/* Now that the connection has been established and all of the objects
are connected to each other, the command may be sent and the data
transfer may begin. */
String ActiveAccountsData = ("\"accounts_active\": (\\d+)");
String ActiveAccountsDataFOUND;
Pattern ActiveAccountsPattern = Pattern.compile(ActiveAccountsData);
Matcher ActiveAccountsMatcher;
String input;
while(i <= 4)
{
out.print(command);
out.flush();
while(in.hasNextLine())
{
input = in.nextLine();
ActiveAccountsMatcher = ActiveAccountsPattern.matcher(input);
if(ActiveAccountsMatcher.find())
{
ActiveAccountsDataFOUND = ActiveAccountsMatcher.group(1);
F_printstream.println(ActiveAccountsDataFOUND);
break;
}
}
i++;
F_printstream.println();
Thread.sleep(5000);
}
}
}
我在想也许该in.hasNextLine()
值被卡在某个地方并且需要更新,但是我找不到将其返回到网站输入开头的方法。