Java股票数据下载

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();

}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你在学习Python数据分析的时候,是否遇到过在这些问题? 别慌!这些都是数据科学入门常见问题。从入门到上手再到解决实际问题,数据科学看似复杂,但如果你掌握了正确的学习方法,完全可以极速入门。 【职场人进阶必备  数据分析/挖掘一点通】 如今的职场上,90%以上的岗位都会涉及数据问题。 以产品文案岗位为例,在一个新产品推向市场之前,文案需要考虑: 此时,可以关注一下市场上已有的相关产品推广数据,如:哪些文案打开率更高?哪些文案转化更好?目标用户的购买习惯如何? 以此作为下一步工作开展的依据,对产品文案工作者来说,可以少走很多弯路。 学会数据分析/挖掘,等于站巨人的肩膀上工作,轻松且高效。 【爬虫、数据分析、数据挖掘知识点三合一】数据问题一网打尽 本课程将知识点悉数融入实战项目,不空谈语法,帮助学员在实践中获取知识,目标是:让学员能自主完成数据采集、数据分析与数据挖掘。 学习完本课程,你可以熟练掌握: 【实战案例超实用,轻松拥有“睡后收入”!】 本课程以股票案例为主线,串联爬虫、数据分析以及数据挖掘多个知识点。 通过实战案例演练,你可以全面掌握股票收益的分析和预判方法,在收获新技能的同时,也有机会获得“睡后收入”! 四大优势: 三重权益:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值