2014年9月7日 星期日

如何簡單快速的編譯並執行JAVA程式-以查詢即時股價為例

和大多數人一樣,我平時撰寫JAVA程式時,都是透過eclipse來編譯並執行。
但如果我只是想小幅修改並執行一支簡單的程式,實在不想勞師動眾去開啟eclipse呢?
或者需要在沒有eclipse的電腦上執行,但實在不想為了一支小程式再去下載佈署eclipse?
其實只要稍稍修改一下JAVA程式,再搭配bat指令即可做到,
當然,執行程式的電腦上必須有適當的JRE,這點我想不用多做說明。

以下以一個從Yahoo!奇摩股市抓特定股票股價的程式為例,說明如何低調的關心投資標的的股價
JAVA程式:stock.java
我是先在eclipse寫好後,去workspace把java檔另存一份,
然後打開另存的java檔,把第一行的package xxx拿掉,就變成這樣:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;

public class stock {
    public static void main(String[] args) {
        try{
            String[] CodeStr = {"0050","2330","2498"};//鍵入股票代號,支數不限
            String theURL = "";
            URL u;
            BufferedReader theHtml;
            String name = "";
            String nowTime = "";
            String Recommand = "";
            String nowPrice = "";
            String nowBuyPrice = "";
            String nowSellPrice = "";
            String nowChange = "";
            String nowDiffRate = "";
            String nowQuantities = "";
            String yesterdayPrice = "";
            String opening = "";
            String high = "";
            String low = "";
            
            for(int z = 0 ; z < 6000 ; z++){//執行次數只要可以cover開盤到收盤的時間即可
                System.out.println("代號\t時間\t成交價\t買價\t賣價\t漲跌\t漲跌幅\t成交量\t昨收\t開盤\t最高\t最低");
            for(int Code = 0 ; Code < CodeStr.length ; Code++ ){
                try{                    
                    theURL = "http://tw.stock.yahoo.com/q/q?s="+CodeStr[Code];
                    u = new URL(theURL);
                    theHtml = new BufferedReader(new InputStreamReader(u.openStream()));
                    String theWholePackage = "";
                    while((theURL = theHtml.readLine())!=null){
                        theWholePackage = theWholePackage + theURL+"\n";    
                    }
                    
                    int s = theWholePackage.indexOf("href=\"/q/bc?s="+CodeStr[Code]+"\">");
                    int t = theWholePackage.indexOf("</a><br><a href=\"/pf/pfsel?stocklist="+CodeStr[Code]+";\"><font size=-1>");
                    name = theWholePackage.substring(s+20,t);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    theWholePackage = theWholePackage.substring(s+44);
                    nowTime = theWholePackage.substring(0,5);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    t = theWholePackage.indexOf("</b></td>");
                    nowPrice = theWholePackage.substring(s+47,t);
                    theWholePackage = theWholePackage.substring(t+8);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    t = theWholePackage.indexOf("</td>");
                    nowBuyPrice = theWholePackage.substring(s+44,t);
                    theWholePackage = theWholePackage.substring(t+4);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    t = theWholePackage.indexOf("</td>");
                    nowSellPrice = theWholePackage.substring(s+44,t);
                    theWholePackage = theWholePackage.substring(t+4);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap><font color=#009900>");
                    theWholePackage = theWholePackage.substring(s+83);
                    t = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    theWholePackage = theWholePackage.substring(t);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    t = theWholePackage.indexOf("</td>");
                    nowQuantities = theWholePackage.substring(s+44,t);
                    theWholePackage = theWholePackage.substring(t+4);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    t = theWholePackage.indexOf("</td>");
                    yesterdayPrice = theWholePackage.substring(s+44,t);
                    theWholePackage = theWholePackage.substring(t+4);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    t = theWholePackage.indexOf("</td>");
                    opening = theWholePackage.substring(s+44,t);
                    theWholePackage = theWholePackage.substring(t+4);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    t = theWholePackage.indexOf("</td>");
                    high = theWholePackage.substring(s+44,t);
                    theWholePackage = theWholePackage.substring(t+4);
                    
                    s = theWholePackage.indexOf("<td align=\"center\" bgcolor=\"#FFFfff\" nowrap>");
                    t = theWholePackage.indexOf("</td>");
                    low = theWholePackage.substring(s+44,t);
                    theWholePackage = theWholePackage.substring(t+4);
                    
                    double ch1 = Double.parseDouble(nowPrice);
                    double ch2 = Double.parseDouble(yesterdayPrice);
                    double ch = 0 ;
                    double chr = 0 ;
                    if(ch1 - ch2 >= 0)ch = ch1 - ch2 + 0.005 ;
                    else ch = ch1 - ch2 - 0.005 ;
                    ch = (int)(ch*1000)/10;
                    ch = ch/100;
                    chr = (ch*100)/ch2;
                    chr = (int)(chr*1000)/10;
                    chr = chr/100;

                    if(ch<0){
                        nowChange = "-"+String.valueOf(-ch);
                        nowDiffRate = "-"+String.valueOf(-chr)+"%";
                    }else{
                        nowChange = "+"+String.valueOf(ch);
                        nowDiffRate = "+"+String.valueOf(chr)+"%";
                    }
                    
                    System.out.println(CodeStr[Code]+"\t"+nowTime+"\t"+nowPrice+"\t"+nowBuyPrice
+"\t"+nowSellPrice+"\t"+nowChange+"\t"+nowDiffRate+"\t"+nowQuantities+"\t"+yesterdayPrice
+"\t"+opening+"\t"+high+"\t"+low);                    

                    
                    Thread.sleep(1000);//停一秒
                }catch(java.io.FileNotFoundException e1){
                    System.out.println(e1);
                    Thread.sleep(1000);
                    continue;
                }catch(java.lang.StringIndexOutOfBoundsException e2){
                    System.out.println(e2);
                    Thread.sleep(1000);
                    continue;
                }
            }
                System.out.println();
                Thread.sleep(1000);//停一秒
            }
        }catch(Exception e){
                System.out.println(e.toString());
        }
    }
}適當的秒數暫停是為了避免畫面跳太快看不清楚,同時也表示對Yahoo!奇摩股市伺服器的尊重。

bat檔:stock.bat,bat的檔名可以隨便取,但前面java的檔名一定要和程式中的class名一樣,雖然是常識,但還是稍微提醒一下。
如下使用cmd指令把java檔先編譯後執行
@echo off
color 0E
javac stock.java
java -classpath . stock
pause
其中,color 0E是用來指定執行畫面的顏色,第一碼是底色,第二碼是字的顏色,可以用的顏色及代碼有:
  0 = 黑色
  1 = 藍色
  2 = 綠色
  3 = 藍綠色
  4 = 紅色
  5 = 紫色
  6 = 黃色
  7 = 白色
  8 = 灰色
  9 = 淡藍色
  A = 淡綠色
  B = 淡藍綠色
  C = 淡紅色
  D = 淡紫色
  E = 淡黃色
  F = 亮白色
可自行視狀況搭配低調而清楚的顏色