⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urllinkexample.java

📁 java 完全探索的随书源码
💻 JAVA
字号:
import java.net.*;import java.io.*;import java.util.*;public class URLLinkExample{  // Default Constructor  public URLLinkExample()  {    super();  }  // Does the token have a "http:" substirng within it  private boolean hasMatch( String token )  {    return token.indexOf( "http:" ) != -1;  }  // Trim the string to something respectful to print out  private String trimURL( String url )  {    String tempStr = null;    int beginIndex = url.indexOf( "http" );    int endIndex = url.length();    tempStr = url.substring( beginIndex, endIndex );    endIndex = tempStr.indexOf( '"' );    if ( endIndex == -1 )      endIndex = tempStr.length();    return tempStr.substring( 0, endIndex );  }  // Go through all of the text returned from a web site and search for links  public Collection searchURL( String urlString )  {    URL url = null;    URLConnection conn = null;    String nextLine = null;    StringTokenizer tokenizer = null;    Collection urlCollection = new ArrayList();    try    {      // Get a new URL object on the url string passed in      url = new URL( urlString );      // open the connection      conn = url.openConnection();      conn.setDoOutput( true );      // Complete the connection      conn.connect();      BufferedReader reader = new BufferedReader( new InputStreamReader( conn.getInputStream() ));      // Go through all the text and check it for being a link to another page      while( (nextLine = reader.readLine()) != null )      {        // Create a tokenizer on each text line        tokenizer = new StringTokenizer( nextLine );        while( tokenizer.hasMoreTokens() )        {          String urlToken = tokenizer.nextToken();          // If the token is a link, add it to a collection          if ( hasMatch( urlToken) )            urlCollection.add( trimURL( urlToken ) );        }      }    }    catch( MalformedURLException ex )    {      System.out.println( "You did not provide a valid URL" );      ex.printStackTrace();    }    catch( IOException ex )    {      ex.printStackTrace();    }    return urlCollection;  }  public static void main(String[] args)  {    if( args.length != 1 )    {      System.out.println( "Usage: java URLLinkExample <http://url>" );      System.exit( -1 );    }    // Get the url from the command line arguments    String url = args[0];    System.out.println( "Searching web site: " + url );    URLLinkExample example = new URLLinkExample();    Collection urlCollection = example.searchURL( url );    // Print out the candiate links    Iterator iter = urlCollection.iterator();    while( iter.hasNext() )    {      System.out.println( iter.next() );    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -