- import java.io.*;
- import java.net.*;
- import java.util.regex.*;
- class UrlDemo
- {
- public static void main(String[] args) throws Exception
- {
- URL url = new URL("http://www.csdn.net");
- URLConnection conn = url.openConnection();
- String rex = "[https]{4,5}[:][/]{2}\\w+(\\.\\w+)+(/\\w+)*(\\.\\w+)?(\\?\\w+\\=\\-?\\w+)*(\\&\\w+\\=\\-?\\w+)*";
- Pattern p = Pattern.compile(rex);
-
- BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\csdn.txt"));
- BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line = null;
- while((line = br.readLine())!=null)
- {
- Matcher m = p.matcher(line);
- while(m.find())
- {
- bw.write(m.group());
- bw.newLine();
- bw.flush();
- }
- }
- br.close();
- bw.close();
- }
- }
复制代码 |
|