Java uses the TimerTask timer to get the specified network data _java

Source: Internet
Author: User
Tags dateformat readline

Copy Code code as follows:

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 ();
Run the T1 task every 1000 milliseconds after 1000 milliseconds
Timer.schedule (t1,1000,5000);
/*try {
Thread.Sleep (10000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
Timer.cancel ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.