>  기사  >  Java  >  Java는 TimerTask 타이머를 사용하여 지정된 네트워크 데이터를 얻습니다.

Java는 TimerTask 타이머를 사용하여 지정된 네트워크 데이터를 얻습니다.

高洛峰
高洛峰원래의
2017-01-11 15:53:531589검색

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class GetYinInfo extends TimerTask {
private void getCOMEXInfo() throws IOException{
String res = "";
SimpleDateFormat dateformat=new SimpleDateFormat("HH:mm:ss");
String df=dateformat.format(new Date());
URL url = new URL("http://www.jb51.netI");
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader bf = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "GBK"));
String line;
while ((line = bf.readLine()) != null) {
res += line;
}
String AGTD[]=res.split(",");
String re[]=AGTD[0].split(""");
System.out.println("COMEX "+df+":"+re[1]);
bf.close();
}
private void getTDInfo() throws IOException{
String res = "";
SimpleDateFormat dateformat=new SimpleDateFormat("HH:mm:ss");
String df=dateformat.format(new Date());
URL url = new URL(http://www.jb51.net);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader bf = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "GBK"));
String line;
while ((line = bf.readLine()) != null) {
res += line;
}
String AGTD[]=res.split(",");
String re[]=AGTD[0].split(""");
System.out.println("AG "+df+":"+re[1]);
bf.close();
}
@Override
public void run() {
try {
getCOMEXInfo();
getTDInfo();
System.out.println("---------------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask t1 = new GetYinInfo();
//1000毫秒后,每隔1000毫秒运行一次t1任务
timer.schedule(t1,1000,5000);
/*try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();*/
}
}

TimerTask 타이머를 사용하여 지정된 네트워크 데이터를 얻는 Java 관련 기사를 더 보려면 PHP 중국어 웹사이트를 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.