大家帮我看一下这个代码:
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient; public class getCode{
   public   static String  readHtml(HttpClient client,String url) throws IOException   {   
   StringBuffer a=new StringBuffer();
   String htmlFile = "";
   
   File f1=new File("E:\\jsp\\test.txt");   
   if(!f1.exists()){    //检查文件是否存在
System.out.print("没有相应的文件");

try 
{
f1.createNewFile();
} catch (IOException e) 
{
e.printStackTrace();
}
   }
   FileWriter fs=new FileWriter(f1);
   
   
   int i=0;
   String str = null;
   GetMethod get = new GetMethod(url);
   try {
   client.executeMethod(get);
   str = get.getResponseBodyAsString();
   }
   catch (HttpException e) {
   e.printStackTrace();
   } 
   catch (IOException e) {
   e.printStackTrace();
   }
   get.releaseConnection();
   
   fs.write(str);
   BufferedReader   reader=new   BufferedReader(new FileReader("E:\\jsp\\test.txt"));   
           String   line=reader.readLine();        
           while(line!=null){   
               line   =   reader.readLine();                         
               String  r="(\\r|\\n|\\s+<td align=\"center\"\\s+>)(.*)(</td>|\\n|\\r)";
               String  r1="(.*共 <span class=\"text_orange\">)(.*)(</span> 页.*)";
Pattern s=Pattern.compile(r);
Matcher m = s.matcher(line);
boolean T=m.matches();
if(T){
    a.append(m.group(2)+",");
    htmlFile=htmlFile+m.group(2)+",";
    //System.out.println("  "+m.group(2));//这儿输出有值
    i++;
    if(i%5==0)
                        a.append("\n");

Pattern s1=Pattern.compile(r1);
    Matcher m1=s1.matcher(line);
    boolean T1=m1.matches();
    if(T1)
    {
    int pagecount=Integer.parseInt(m1.group(2)) ;
    System.out.println("--------总页数-------:"+pagecount);
    }
   
           }   System.out.println("名称:"+htmlFile);//在这儿输出htmlFile也是空的()不过好像根本就没有输
   return a.toString();
   }  public static void main(String[] args) throws IOException{
String[] aa=null;

HttpClient client = new HttpClient();

client.getHostConfiguration().setHost("www.12582.com", 80);

readHtml( client,"/cqnxt/priceMarketingAction.do?eachOrAll=all&serviceName=jghq&globalLocationid=1168");

String str = readHtml(client, "/cqnxt/turnPageAction.do?index=2&keyWord2=&area=null");

aa=str.split(",");

System.out.println("名称为:\n");

for(int i=0;i<aa.length;i++)
{System.out.print(aa[i]+"---");}
}                          //测试          
 
 }测试有值的..只是在while里面有值,出了while的范围就没有值 了.好像没有返回一样..根本就没有输出

解决方案 »

  1.   

    import java.io.IOException;
    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.methods.GetMethod;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import org.apache.commons.httpclient.HttpClient;public class getCode{
    public   static String  readHtml(HttpClient client,String url) throws IOException   {   
    StringBuffer a=new StringBuffer();
    String htmlFile = "";File f1=new File("e:\\jsp\\test.txt");   
    if(!f1.exists()){    //检查文件是否存在
    System.out.print("没有相应的文件");try 
    {
    f1.createNewFile();
    } catch (IOException e) 
    {
    e.printStackTrace();
    }
    }
    FileWriter fs=new FileWriter(f1);
    int i=0;
    String str = null;
    GetMethod get = new GetMethod(url);
    try {
    client.executeMethod(get);
    str = get.getResponseBodyAsString();
    }
    catch (HttpException e) {
    e.printStackTrace();

    catch (IOException e) {
    e.printStackTrace();
    }
    get.releaseConnection();fs.write(str);
    BufferedReader   reader=new   BufferedReader(new FileReader("e:\\jsp\\test.txt"));   
    String line = null;//String   line=reader.readLine();        
    while((line = reader.readLine())!=null){                      
    String  r="(\\r|\\n|\\s+<td align=\"center\"\\s+>)(.*)(</td>|\\n|\\r)";
    String  r1="(.*共 <span class=\"text_orange\">)(.*)(</span> 页.*)";
    Pattern s=Pattern.compile(r);
    Matcher m = s.matcher(line);
    boolean T=m.matches();
    if(T){
    a.append(m.group(2)+",");
    htmlFile=htmlFile+m.group(2)+",";
    //System.out.println("  "+m.group(2));//这儿输出有值
    i++;
    if(i%5==0)
    a.append("\n");

    Pattern s1=Pattern.compile(r1);
    Matcher m1=s1.matcher(line);
    boolean T1=m1.matches();
    if(T1)
    {
    int pagecount=Integer.parseInt(m1.group(2)) ;
    System.out.println("--------总页数-------:"+pagecount);
    }}   System.out.println("名称:"+htmlFile);//在这儿输出htmlFile也是空的()不过好像根本就没有输
    return a.toString();
    } public static void main(String[] args) throws IOException{
    String[] aa=null; HttpClient client = new HttpClient();client.getHostConfiguration().setHost("www.12582.com", 80);readHtml( client,"/cqnxt/priceMarketingAction.do?eachOrAll=all&serviceName=jghq&globalLocationid=1168");String str = readHtml(client, "/cqnxt/turnPageAction.do?index=2&keyWord2=&area=null");aa=str.split(",");System.out.println("名称为:\n"); for(int i=0;i<aa.length;i++)
    {System.out.print(aa[i]+"---");}
    }                          //测试          }
      

  2.   

    谢谢你了,我知道是怎么回事了,和你这个一样的
    我把那个line = reader.readLine()放到下面去了就OK了..放在前面报了一个异常,可是我最开始不懂.后来才明白
    再次谢谢你