夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          JAVA代碼:
          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileWriter;
          import java.io.InputStreamReader;
          import java.net.URL;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;

          public class W
          {
              
          public static void main(String[] args)
              
          {
                  
          /*
                   * String[] paras = new String[]
                   * {"
          http://m.weather.com.cn/data/101020100.html", "g:/abc.txt" };
                   
          */

                  
          new W().working(args);
              }

              
              
          /**
               * <ul>
               * <li>Description:[總調用程序]</li>
               * <li>Created by [Huyvanpull] [2013-3-14]</li>
               * <li>Midified by [modifier] [modified time]</li>
               * </ul>
               * 
               * 
          @param paras
               
          */

              
          public void working(String[] paras)
              
          {
                  StringBuffer result 
          = new StringBuffer();
                  
          if (paras != null && paras.length == 2)
                  
          {
                      result.append(paras[
          0]).append(",");
                      result.append(getDateStr()).append(
          ",");
                      
                      String tempStr 
          = "";
                      
          try
                      
          {
                          tempStr 
          = this.sendUrl(paras[0]);
                      }

                      
          catch (Exception ex)
                      
          {
                          ex.printStackTrace();
                      }

                      result.append(tempStr);
                  }

                  
          else
                  
          {
                      result.append(
          "參數(shù)不正確");
                  }

                  result.append(System.getProperty(
          "line.separator"));
                  System.out.println(result);
                  writeFile(paras[
          1], result.toString());
              }

              
              
          /**
               * <ul>
               * <li>Description:[根據(jù)網址得到內容]</li>
               * <li>Created by [Huyvanpull] [2013-3-14]</li>
               * <li>Midified by [modifier] [modified time]</li>
               * </ul>
               * 
               * 
          @param urlStr
               * 
          @return
               
          */

              
          public String sendUrl(String urlStr)
              
          {
                  BufferedReader in 
          = null;
                  StringBuffer temp 
          = new StringBuffer();
                  
          try
                  
          {
                      URL yahoo 
          = new URL(urlStr);
                      in 
          = new BufferedReader(new InputStreamReader(yahoo.openStream(),
                              
          "UTF-8"));
                      String inputLine;
                      
          while ((inputLine = in.readLine()) != null)
                          temp.append(inputLine);
                  }

                  
          catch (Exception ex)
                  
          {
                      ex.printStackTrace();
                  }

                  
          finally
                  
          {
                      
          if (in != null)
                      
          {
                          
          try
                          
          {
                              in.close();
                          }

                          
          catch (Exception ex)
                          
          {
                              ex.printStackTrace();
                          }

                      }

                  }

                  
          return temp.toString();
              }

              
              
          /**
               * <ul>
               * <li>Description:[寫字符串到文件]</li>
               * <li>Created by [Huyvanpull] [2013-3-14]</li>
               * <li>Midified by [modifier] [modified time]</li>
               * </ul>
               * 
               * 
          @param filePath
               * 
          @param content
               
          */

              
          public static void writeFile(String filePath, String content)
              
          {
                  FileWriter fw 
          = null;
                  
          try
                  
          {
                      File file 
          = new File(filePath);
                      fw 
          = new FileWriter(file, true);
                      fw.write(content);
                  }

                  
          catch (Exception ex)
                  
          {
                      ex.printStackTrace();
                  }

                  
          finally
                  
          {
                      
          try
                      
          {
                          
          if (fw != null)
                          
          {
                              fw.close();
                          }

                      }

                      
          catch (Exception ex)
                      
          {
                          ex.printStackTrace();
                      }

                  }

              }

              
              
          public String getDateStr()
              
          {
                  SimpleDateFormat simpleDateFormate 
          = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  
          return simpleDateFormate.format(Calendar.getInstance().getTime());
              }

              
          }



          調用代碼:
          java -jar f:/w.jar http://m.weather.com.cn/data/101020100.html f:/kkk.txt
          posted on 2013-03-15 10:15 HUIKK 閱讀(548) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 海林市| 磐安县| 广德县| 蒙山县| 晋城| 宜州市| 泰州市| 英德市| 全南县| 义乌市| 哈尔滨市| 扎囊县| 永宁县| 玉环县| 白山市| 启东市| 根河市| 外汇| 潜江市| 临澧县| 辽阳县| 微博| 渝中区| 绵竹市| 时尚| 康马县| 黄浦区| 定西市| 枣庄市| 古交市| 新平| 上饶县| 繁峙县| 普定县| 汾西县| 渑池县| 新密市| 左云县| 灵丘县| 寿阳县| 滕州市|