package batchic;import java.io.PrintWriter;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;public class bicMain {
    private static String     strRootFilePate = "";
    public static void main(String[] args) {
        strRootFilePate = bicUtil.GetRootPath();
        System.out.println("********************************************************");
        System.out.println("BATCH MAIL SENDER  (C)  2003-8   1.0.1     YangGuoqing");
        System.out.println("");
        System.out.println("MODIFY HISTORY");
        System.out.println("1.0--1.0.1 : ADD AUTO-DETECT APPLICATION ROOT PATH");
        System.out.println("           : CHANGE MAIL TYPE TO 0,1,3,9 FOR SENDING ");
        System.out.println("             DIFFERENT SUBJECT MAIL");
        System.out.println("********************************************************");
        System.out.println("");
        char chrMailType = 0;
        while(true){
            if(chrMailType != 10 && chrMailType != 13){
                System.out.println("PLEASE SELECT \"0\" , \"1\" , \"3\" or \"9\" FROM YOUR KEYBOARD");
                System.out.println("  0:  MAIL DATE IS [TODAY]");
                System.out.println("  1:  MAIL DATE IS [YESTODAY]");
                System.out.println("  3:  MAIL DATE IS [LAST WEEKEND]");
                System.out.println("  9:  QUIT");
                System.out.print("CHOOSE TYPE:");
            }
            try {
                chrMailType  =  (char)System.in.read();
            }
            catch (Exception ex) {
                System.out.println("[Error:]Can't read from keyboard input.");
                ex.printStackTrace();
            }
            if(chrMailType !='0' && chrMailType !='1' && chrMailType !='3' && chrMailType !='9'){
                if(chrMailType != 10 && chrMailType != 13){
                    System.out.print("KEYBOARD INPUT ERROR["+ chrMailType +"],");
                }
            }else if(chrMailType == '9'){
                System.exit(0);
            }else{
                break;
            }
        }
        System.out.print("SET MAIL CONFIG");
        String strFilePate = strRootFilePate + "config" + File.separator;
        bicProp prop       = new bicProp(strFilePate);
        int    iHostPort   = prop.getIntProperty("mail.host.port");
        String strHostIP   = prop.getProperty("mail.host.ip");
        String strSender   = prop.getProperty("mail.sender");
        String strFrom     = prop.getProperty("mail.from");
        if(!bicUtil.IsNull(strSender)){
            strFrom = "\"" + strSender + "\" <" + strFrom + ">";
        }
        String strTo       = prop.getProperty("mail.to");
        String strSubject  = prop.getProperty("mail.subject");
        String strEncode   = prop.getProperty("mail.encode");
        System.out.print(".");
        if(chrMailType =='0' || chrMailType =='0'){
            strSubject = bicUtil.Replace(strSubject,"[DATE]",bicUtil.GetCurrentDate());
        }else if(chrMailType == '1'){
            strSubject = bicUtil.Replace(strSubject,"[DATE]",bicUtil.GetSpecialDate(0,0,-1));
        }else if(chrMailType == '3'){
            strSubject = bicUtil.Replace(strSubject,"[DATE]",bicUtil.GetSpecialDate(0,0,-3));
        }
        System.out.print(".");
        String strTextName = prop.getProperty("mail.text");
        String strText     = "";
        bicFile mailText   = new bicFile(strFilePate,strTextName);
        System.out.print(".");
        if(!mailText.exists()){
            System.out.println("Error:mail text is not exists.");
            return;
        }else{
            String strLine;
            try {
                mailText.openReadFile(strEncode);
                while((strLine = mailText.readLine()) != null){
                    System.out.print(".");
                    strText += strLine + "\r\n";
                }
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        System.out.println(".");
        System.out.print("MAIL SENDING");
        try {
            bicMail mail = new bicMail(strRootFilePate);
            if(iHostPort != 0){
                mail.setMailPort(iHostPort);
            }
            if(!bicUtil.IsNull(strHostIP)){
                mail.setMailSrvAddr(strHostIP);
            }
            System.out.print(".");
            mail.setEncName(strEncode);
            System.out.print(".");
            mail.setMailFrom(strFrom);
            System.out.print(".");
            mail.setMailTo(strTo);
            System.out.print(".");
            mail.setMailSubject(strSubject);
            System.out.print(".");
            mail.setMailText(strText);
            System.out.print(".");
            mail.send();
            System.out.println(".");
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("MAIL SEND FINISH,QUIT AUTOMATIC.");    }    private void AppWait(int iTime){
        try {
            Thread.sleep(iTime);
        }
        catch (Exception ex) {
            System.out.println("[ERROR]WAIT ERROR.");
        }
    }
}

解决方案 »

  1.   

    System.in is an object of InputStream class. So, you can use its read() methods for byte based input from keyboard or from redirected file.int n; //actually a byte value
    n = System.in.read();For above code, if you push 'a' on the keyboard, the value of n is 97 which is the code value of character 'a'.The number one console Java cliche is:BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine(); //string input from keyboardThen, if all the characters in the string 'line' is digit, you can get numerical value using another Java cliche:int n = Integer.parseInt(line);
    or
    double d = Double.parseDouble(line);
    etc.All most all of the above cliches geneate Exceptions. You must try/catch or throws them otherwise you get compiler error.