您的位置:首页 > 编程语言 > Java开发

Java股票数据下载

2016-07-05 05:01 211 查看
package main;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.Iterator;

import java.util.List;

import java.util.Timer;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

/**

* <p>Title: RunStock2.java</p>

* <p>Description:定时插入mydql数据库 </p>

* <p>Copyright: Copyright (c) 2013</p>

* <p>Company: zyf</p>

* @author zyf

* @date 2014-8-3

* @version 1.0

 */

public class RunStock {
long starttime;

public void RunStock(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
java.util.Calendar c1=java.util.Calendar.getInstance();
long s = new Date().getTime();
String dates = sdf1.format(new Date());
String dates1 = sdf.format(new Date());
System.out.println(dates1);
String ss =  dates + " 09:00:00" ;
System.out.println(ss);
try {
Date sdate = sdf.parse(ss);
long l ;

System.out.println("开始" + sdate.getTime());
System.out.println("结束" + new Date().getTime());
System.out.println(dates);

l =   sdate.getTime() - new Date().getTime();
System.out.println("绝对值=" + Math.abs(l));
if(l<0){
starttime = Math.abs(l)+ 12*3600*1000;
}else{
starttime = l;
}

System.out.print(l);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

MyTask mt = new MyTask();
mt.run();
mt.Command();

}

public static void main(String[] args){
RunStock rs = new RunStock();
rs.RunStock();
}

}

class MyTask{

public void run(){
List<String> list = null;
try {
list = setStockCode();
getStockTextinfo(list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public  boolean getStockTextinfo(List<String> list) throws Exception {
// TODO Auto-generated method stub
boolean bl = false;
//List<String> list = setStockCode();
Iterator<String> it = list.iterator();
HttpClient client = new DefaultHttpClient();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println("----------start:=" + getDate());
while(it.hasNext()){
String str1 = it.next();
String url = "http://hq.sinajs.cn/list=" + str1;

//System.out.println(url);
HttpGet request = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(request);
// 得到结果,进行解释
if (response.getStatusLine().getStatusCode() == 200) {
// 连接成功
String strResponse = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);// 将响应结果转换为String类型
String str123 = str1 + ',' +strResponse.substring(strResponse.indexOf('"')+1,strResponse.lastIndexOf('"'));
String [] strlength = str123.split(",");
if(strlength.length == 34){
String str_1 = str123 + ',' + sdf1.format(new java.util.Date()) +'\n';
doWriteText(str_1);
}
bl = true;
} else {
// 没有请求成功
System.out.println("请求的连接状态没有ok");
}

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.abort();
}

System.out.println("----------end:=" + getDate());
return bl;
}

public  List<String> setStockCode() throws Exception {

List<String> alist = new ArrayList<String>(); //从文本存到List
File file = new File("D:/zyf_Workspaces/Stockv7/src/main/txt/stock_data.txt");//读文件
//File file = new File("/root/stock/stock_d/stock_data.txt");//读文件
FileInputStream inputstream;
BufferedReader reader = null;
String strtemp = "";
try {
inputstream = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(inputstream));
int j = 0;
while((strtemp = reader.readLine()) != null){
alist.add(strtemp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
       if(reader != null){
           try {
               reader.close();
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
   }
return alist;
}

public String getDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
String sdate = sdf.format(new java.util.Date());
return sdate;
}

public boolean doWriteText(String str) throws Exception {
// TODO Auto-generated method stub
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
String sdate = sdf1.format(new java.util.Date());
//String filepath = "/root/stock/stock_d/data/" + sdate + ".dat";
String filepath = "d:/data/" + sdate + ".dat";
File file = new File(filepath);
if(!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file,true);
out.write(str.getBytes("GBK"));
out.close();
return true;
}

public void Command(){
try {
String shpath="/root/stock/stock_d/ImpStock_d.sh";   //程序路径

   Process process =null;

   String command1 = shpath;
   process = Runtime.getRuntime().exec(command1);
   
   try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: