梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  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(
"参数不正确");
        }

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

    
    
/**
     * <ul>
     * <li>Description:[根据网址得到内容]</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 阅读(536) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: