java使用TimerTask定時器獲取指定網(wǎng)絡(luò)數(shù)據(jù)
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.dbjr.com.cnI");
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.dbjr.com.cn);
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任務(wù)
timer.schedule(t1,1000,5000);
/*try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();*/
}
}
相關(guān)文章
Java實現(xiàn)ftp文件上傳下載解決慢中文亂碼多個文件下載等問題
這篇文章主要介紹了Java實現(xiàn)ftp文件上傳下載解決慢中文亂碼多個文件下載等問題的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10java使用短信設(shè)備發(fā)送sms短信的示例(java發(fā)送短信)
這篇文章主要介紹了java使用短信設(shè)備發(fā)送sms短信的示例(java發(fā)送短信),需要的朋友可以參考下2014-04-04springboot基于keytool實現(xiàn)https的雙向認(rèn)證示例教程
這篇文章主要介紹了springboot基于keytool實現(xiàn)https的雙向認(rèn)證,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06java多線程之線程,進程和Synchronized概念初解
這篇文章主要介紹了java多線程之線程,進程和Synchronized概念初解,涉及進程與線程的簡單概念,實現(xiàn)多線程的方式,線程安全問題,synchronized修飾符等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。2017-11-11Spring擴展點之BeanFactoryPostProcessor詳解
這篇文章主要介紹了Spring擴展點之BeanFactoryPostProcessor詳解,Spring的設(shè)計非常優(yōu)雅,有很多的擴展點供我們對項目進行擴展,今天學(xué)習(xí)一下Spring其中擴展點之一的BeanFactoryPostProcessor,需要的朋友可以參考下2023-11-11HttpServletRequest對象常用功能_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了HttpServletRequest對象常用功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07