diff --git a/CountMostImport/.idea/compiler.xml b/CountMostImport/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/CountMostImport/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CountMostImport/.idea/copyright/profiles_settings.xml b/CountMostImport/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/CountMostImport/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/CountMostImport/.idea/libraries/KotlinJavaRuntime.xml b/CountMostImport/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..c630c0b --- /dev/null +++ b/CountMostImport/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/CountMostImport/.idea/misc.xml b/CountMostImport/.idea/misc.xml new file mode 100644 index 0000000..c6d8fb7 --- /dev/null +++ b/CountMostImport/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CountMostImport/.idea/modules.xml b/CountMostImport/.idea/modules.xml new file mode 100644 index 0000000..f5092c1 --- /dev/null +++ b/CountMostImport/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/CountMostImport/.idea/workspace.xml b/CountMostImport/.idea/workspace.xml new file mode 100644 index 0000000..46bec5a --- /dev/null +++ b/CountMostImport/.idea/workspace.xml @@ -0,0 +1,525 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1467297922760 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CountMostImport/CountMostImport.iml b/CountMostImport/CountMostImport.iml new file mode 100644 index 0000000..245d342 --- /dev/null +++ b/CountMostImport/CountMostImport.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/CountMostImport/out/production/CountMostImport/CountImport.class b/CountMostImport/out/production/CountMostImport/CountImport.class new file mode 100644 index 0000000..c1d8b3f Binary files /dev/null and b/CountMostImport/out/production/CountMostImport/CountImport.class differ diff --git a/CountMostImport/out/production/CountMostImport/CountMostImport.class b/CountMostImport/out/production/CountMostImport/CountMostImport.class new file mode 100644 index 0000000..ff8e2fb Binary files /dev/null and b/CountMostImport/out/production/CountMostImport/CountMostImport.class differ diff --git a/CountMostImport/out/production/CountMostImport/JavaFileFilter.class b/CountMostImport/out/production/CountMostImport/JavaFileFilter.class new file mode 100644 index 0000000..51ac5b3 Binary files /dev/null and b/CountMostImport/out/production/CountMostImport/JavaFileFilter.class differ diff --git a/CountMostImport/src/CountImport.java b/CountMostImport/src/CountImport.java new file mode 100644 index 0000000..35582db --- /dev/null +++ b/CountMostImport/src/CountImport.java @@ -0,0 +1,87 @@ +/** + * Created by zhaoxin on 16-6-30. + */ +import java.util.*; +import java.util.Map.Entry; +import java.io.File; +import java.lang.Integer; + +public class CountImport { + private Hashtable hashtable; + public CountImport() + { + this.hashtable=new Hashtable(); + + } + public void count(File java_file) + { + try + { + Scanner scan = new Scanner(java_file); + String line; + String[] tokens; + while(scan.hasNextLine()) + { + line=scan.nextLine(); + line.trim(); + if(line.startsWith("import") && !line.contains("*")) + { + tokens=line.split(" "); + if(hashtable.containsKey(tokens[1])) + { + hashtable.replace(tokens[1],hashtable.get(tokens[1])+1); + } + else + { + hashtable.put(tokens[1],1); + } + } + } + } + catch (java.io.FileNotFoundException e) + { + System.out.println("文件不存在"); + System.exit(0); + } + + } + public void getMostImport() + { + ArrayList> sequence_line=new ArrayList>(); //单调队列 + Iterator> ite=hashtable.entrySet().iterator(); + while(ite.hasNext()) + { + Entry current_entry=ite.next(); + for(int i=0;i<10;i++) + { + try + { + if (sequence_line.get(i).getValue().compareTo(current_entry.getValue())<0) + { + sequence_line.add(i,current_entry); + break; + } + } + catch (IndexOutOfBoundsException e) + { + sequence_line.add(i,current_entry); + break; + } + } + + } + System.out.println("import 最多的10个类是:"); + for (int i=0;i<10;i++) + { + try + { + System.out.println(sequence_line.get(i).getKey()+"被import"+sequence_line.get(i).getValue()+"次"); + } + catch (IndexOutOfBoundsException e) + { + System.out.println("被引用的类不足10个"); + break; + } + } + } +} diff --git a/CountMostImport/src/CountMostImport.java b/CountMostImport/src/CountMostImport.java new file mode 100644 index 0000000..d7798c6 --- /dev/null +++ b/CountMostImport/src/CountMostImport.java @@ -0,0 +1,33 @@ +/** + * Created by zhaoxin on 16-6-30. + */ +import java.io.File; +import java.lang.String; +import java.util.Scanner; +import java.lang.System; + +public class CountMostImport { + + public static void main(String[] args) + { + Scanner scan=new Scanner(System.in); + System.out.print("输入目录名:"); + String file_name=scan.next(); + File directory=new File(file_name); + if(!directory.exists() || !directory.isDirectory()) + { + System.out.println("目录不存在或错误"); + System.exit(0); + } + JavaFileFilter java_file_filter=new JavaFileFilter(); + File[] java_files=directory.listFiles(java_file_filter); + CountImport cout_import=new CountImport(); + for (File file : java_files) + { + cout_import.count(file); + } + cout_import.getMostImport(); + } + +} + diff --git a/CountMostImport/src/JavaFileFilter.java b/CountMostImport/src/JavaFileFilter.java new file mode 100644 index 0000000..14b0f35 --- /dev/null +++ b/CountMostImport/src/JavaFileFilter.java @@ -0,0 +1,18 @@ +/** + * Created by zhaoxin on 16-6-30. + */ +import java.io.File; +import java.io.FilenameFilter; +import java.lang.String; + +public class JavaFileFilter implements FilenameFilter{ + public boolean accept(File dir,String name) + { + if(name.endsWith(".java")) + { + return true; + } + else + return false; + } +} diff --git a/CountMostImport/test/CountImport.java b/CountMostImport/test/CountImport.java new file mode 100644 index 0000000..0e0e598 --- /dev/null +++ b/CountMostImport/test/CountImport.java @@ -0,0 +1,87 @@ +/** + * Created by zhaoxin on 16-6-30. + */ +import java.util.*; +import java.util.Map.Entry; +import java.io.File; +import java.lang.Integer; + +public class CountImport { + private Hashtable hashtable; + public CountImport() + { + this.hashtable=new Hashtable(); + + } + public void count(File java_file) + { + try + { + Scanner scan = new Scanner(java_file); + String line; + String[] tokens; + while(scan.hasNextLine()) + { + line=scan.nextLine(); + line.trim(); + if(line.startsWith("import")) + { + tokens=line.split(" "); + if(hashtable.containsKey(tokens[1])) + { + hashtable.replace(tokens[1],hashtable.get(tokens[1])+1); + } + else + { + hashtable.put(tokens[1],1); + } + } + } + } + catch (java.io.FileNotFoundException e) + { + System.out.println("文件不存在"); + System.exit(0); + } + + } + public void getMostImport() + { + ArrayList> sequence_line=new ArrayList>(); //单调队列 + Iterator> ite=hashtable.entrySet().iterator(); + while(ite.hasNext()) + { + Entry current_entry=ite.next(); + for(int i=0;i<10;i++) + { + try + { + if (sequence_line.get(i).getValue().compareTo(current_entry.getValue())<0) + { + sequence_line.add(i,current_entry); + break; + } + } + catch (IndexOutOfBoundsException e) + { + sequence_line.add(i,current_entry); + break; + } + } + + } + System.out.println("import 最多的10个类是:"); + for (int i=0;i<10;i++) + { + try + { + System.out.println(sequence_line.get(i).getKey()+"被import"+sequence_line.get(i).getValue()+"次"); + } + catch (IndexOutOfBoundsException e) + { + System.out.println("被引用的类不足10个"); + break; + } + } + } +} diff --git a/CountMostImport/test/CountMostImport.java b/CountMostImport/test/CountMostImport.java new file mode 100644 index 0000000..d7798c6 --- /dev/null +++ b/CountMostImport/test/CountMostImport.java @@ -0,0 +1,33 @@ +/** + * Created by zhaoxin on 16-6-30. + */ +import java.io.File; +import java.lang.String; +import java.util.Scanner; +import java.lang.System; + +public class CountMostImport { + + public static void main(String[] args) + { + Scanner scan=new Scanner(System.in); + System.out.print("输入目录名:"); + String file_name=scan.next(); + File directory=new File(file_name); + if(!directory.exists() || !directory.isDirectory()) + { + System.out.println("目录不存在或错误"); + System.exit(0); + } + JavaFileFilter java_file_filter=new JavaFileFilter(); + File[] java_files=directory.listFiles(java_file_filter); + CountImport cout_import=new CountImport(); + for (File file : java_files) + { + cout_import.count(file); + } + cout_import.getMostImport(); + } + +} + diff --git a/CountMostImport/test/JavaFileFilter (copy).txt b/CountMostImport/test/JavaFileFilter (copy).txt new file mode 100644 index 0000000..14b0f35 --- /dev/null +++ b/CountMostImport/test/JavaFileFilter (copy).txt @@ -0,0 +1,18 @@ +/** + * Created by zhaoxin on 16-6-30. + */ +import java.io.File; +import java.io.FilenameFilter; +import java.lang.String; + +public class JavaFileFilter implements FilenameFilter{ + public boolean accept(File dir,String name) + { + if(name.endsWith(".java")) + { + return true; + } + else + return false; + } +} diff --git a/CountMostImport/test/JavaFileFilter.java b/CountMostImport/test/JavaFileFilter.java new file mode 100644 index 0000000..14b0f35 --- /dev/null +++ b/CountMostImport/test/JavaFileFilter.java @@ -0,0 +1,18 @@ +/** + * Created by zhaoxin on 16-6-30. + */ +import java.io.File; +import java.io.FilenameFilter; +import java.lang.String; + +public class JavaFileFilter implements FilenameFilter{ + public boolean accept(File dir,String name) + { + if(name.endsWith(".java")) + { + return true; + } + else + return false; + } +} diff --git a/EffectiveLines/.idea/compiler.xml b/EffectiveLines/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/EffectiveLines/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/EffectiveLines/.idea/copyright/profiles_settings.xml b/EffectiveLines/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/EffectiveLines/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/EffectiveLines/.idea/libraries/KotlinJavaRuntime.xml b/EffectiveLines/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..c630c0b --- /dev/null +++ b/EffectiveLines/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/EffectiveLines/.idea/misc.xml b/EffectiveLines/.idea/misc.xml new file mode 100644 index 0000000..c6d8fb7 --- /dev/null +++ b/EffectiveLines/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/EffectiveLines/.idea/modules.xml b/EffectiveLines/.idea/modules.xml new file mode 100644 index 0000000..79ef913 --- /dev/null +++ b/EffectiveLines/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/EffectiveLines/.idea/workspace.xml b/EffectiveLines/.idea/workspace.xml new file mode 100644 index 0000000..2c48bf4 --- /dev/null +++ b/EffectiveLines/.idea/workspace.xml @@ -0,0 +1,477 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1467295368915 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/EffectiveLines/EffectiveLines.iml b/EffectiveLines/EffectiveLines.iml new file mode 100644 index 0000000..245d342 --- /dev/null +++ b/EffectiveLines/EffectiveLines.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/EffectiveLines/out/production/EffectiveLines/EffectiveLines.class b/EffectiveLines/out/production/EffectiveLines/EffectiveLines.class new file mode 100644 index 0000000..f7d05f3 Binary files /dev/null and b/EffectiveLines/out/production/EffectiveLines/EffectiveLines.class differ diff --git a/EffectiveLines/src/EffectiveLines.java b/EffectiveLines/src/EffectiveLines.java new file mode 100644 index 0000000..65337da --- /dev/null +++ b/EffectiveLines/src/EffectiveLines.java @@ -0,0 +1,46 @@ +/** + * Created by zhaoxin on 16-6-30. + */ +import java.lang.String; +import java.util.Scanner; +import java.lang.System; +import java.io.File; +public class EffectiveLines { + public static void main(String[] args) + { + System.out.println("用户的当前工作目录:/n"+System.getProperty("user.dir")); + Scanner scan=new Scanner(System.in); + System.out.println("请输入要检测的java文件"); + String java_name=scan.next(); + File java_file=new File(java_name); + if (!java_file.exists()) //如果输入文件名无效 + { + System.out.println("输入文件不存在"); + System.exit(0); + } + int num=0; + try + { + scan=new Scanner(java_file); + while(scan.hasNextLine()) + { + String line=scan.nextLine(); + line.trim(); + if(!line.isEmpty() && !line.startsWith("//")) //检测是否有单行注释 + { + num++; + } + } + + } + // + // + catch(java.io.FileNotFoundException exc) + { + System.out.println("输入文件未找到"); + System.exit(0); + } + String result=String.format("文件中的有效行数为%d行",num); + System.out.println(result); + } +} diff --git a/ExchangeRate/.idea/.name b/ExchangeRate/.idea/.name new file mode 100644 index 0000000..3ed68d2 --- /dev/null +++ b/ExchangeRate/.idea/.name @@ -0,0 +1 @@ +ExchangeRate \ No newline at end of file diff --git a/ExchangeRate/.idea/compiler.xml b/ExchangeRate/.idea/compiler.xml new file mode 100644 index 0000000..a852314 --- /dev/null +++ b/ExchangeRate/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/copyright/profiles_settings.xml b/ExchangeRate/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/ExchangeRate/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/encodings.xml b/ExchangeRate/.idea/encodings.xml new file mode 100644 index 0000000..d821048 --- /dev/null +++ b/ExchangeRate/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/libraries/jexcelapi.xml b/ExchangeRate/.idea/libraries/jexcelapi.xml new file mode 100644 index 0000000..674161c --- /dev/null +++ b/ExchangeRate/.idea/libraries/jexcelapi.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/libraries/lib.xml b/ExchangeRate/.idea/libraries/lib.xml new file mode 100644 index 0000000..cd8670c --- /dev/null +++ b/ExchangeRate/.idea/libraries/lib.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/libraries/lib1.xml b/ExchangeRate/.idea/libraries/lib1.xml new file mode 100644 index 0000000..5d66a56 --- /dev/null +++ b/ExchangeRate/.idea/libraries/lib1.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/libraries/lib2.xml b/ExchangeRate/.idea/libraries/lib2.xml new file mode 100644 index 0000000..3714b28 --- /dev/null +++ b/ExchangeRate/.idea/libraries/lib2.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/libraries/src0_82.xml b/ExchangeRate/.idea/libraries/src0_82.xml new file mode 100644 index 0000000..bc2ccb3 --- /dev/null +++ b/ExchangeRate/.idea/libraries/src0_82.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/misc.xml b/ExchangeRate/.idea/misc.xml new file mode 100644 index 0000000..1110500 --- /dev/null +++ b/ExchangeRate/.idea/misc.xml @@ -0,0 +1,168 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/modules.xml b/ExchangeRate/.idea/modules.xml new file mode 100644 index 0000000..0147aad --- /dev/null +++ b/ExchangeRate/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/scopes/scope_settings.xml b/ExchangeRate/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/ExchangeRate/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/uiDesigner.xml b/ExchangeRate/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/ExchangeRate/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/vcs.xml b/ExchangeRate/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/ExchangeRate/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ExchangeRate/.idea/workspace.xml b/ExchangeRate/.idea/workspace.xml new file mode 100644 index 0000000..a59fc1c --- /dev/null +++ b/ExchangeRate/.idea/workspace.xml @@ -0,0 +1,714 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1468031900529 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.7 + + + + + + + + ExchangeRate + + + + + + + + + + + + + + + jexcelapi + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/ExchangeRate.iml b/ExchangeRate/ExchangeRate.iml new file mode 100644 index 0000000..52f6c88 --- /dev/null +++ b/ExchangeRate/ExchangeRate.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ExchangeRate/ExchangeRate.xls b/ExchangeRate/ExchangeRate.xls new file mode 100644 index 0000000..f8c90c1 Binary files /dev/null and b/ExchangeRate/ExchangeRate.xls differ diff --git a/ExchangeRate/out/production/ExchangeRate/ExchangeRate.class b/ExchangeRate/out/production/ExchangeRate/ExchangeRate.class new file mode 100644 index 0000000..c1d5010 Binary files /dev/null and b/ExchangeRate/out/production/ExchangeRate/ExchangeRate.class differ diff --git a/ExchangeRate/src/ExchangeRate.java b/ExchangeRate/src/ExchangeRate.java new file mode 100644 index 0000000..3b4df80 --- /dev/null +++ b/ExchangeRate/src/ExchangeRate.java @@ -0,0 +1,216 @@ +/** + * Created by zhaoxin on 16-7-9. + */ +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.DomElement; +import com.gargoylesoftware.htmlunit.html.HtmlAnchor; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import jxl.Workbook; +import jxl.write.Label; +import jxl.write.WritableSheet; +import jxl.write.WritableWorkbook; +import org.htmlparser.Parser; +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class ExchangeRate { + + private String url_home="http://www.pbc.gov.cn/zhengcehuobisi/125207/125217/125925/17105"; + + private ArrayList link_url_list =new ArrayList(); //一级目录list + + private ArrayList data_url_list =new ArrayList(); //二级目录list + + private ArrayList USDollarExchange=new ArrayList(); + + private ArrayList HongKongDollarExchange=new ArrayList(); + + private ArrayList EuropeDollarExchange=new ArrayList(); + + WritableWorkbook xls; + + private int url_num_perpage=20; //一页有20条有效的url + + public static void main(String[] args) + { + ExchangeRate exchange_rate=new ExchangeRate(); + exchange_rate.getUrlDataList(30); + exchange_rate.parseData(); + } + + private void parseData() + { + try + { + WritableWorkbook xls= Workbook.createWorkbook(new File("ExchangeRate.xls")); + WritableSheet sheet=xls.createSheet("FirstSheet",0); + for (Iterator ite= data_url_list.iterator();ite.hasNext();) + { + try + { + WebClient client=new WebClient(); + client.getOptions().setCssEnabled(true); + client.getOptions().setJavaScriptEnabled(true); + HtmlPage page=client.getPage(ite.next()); + DomElement table=page.getElementById("zoom"); + DomElement first_chiled=table.getFirstElementChild(); + String data=first_chiled.getTextContent(); + String[] paterns=data.split(","); + for (String tem:paterns) + { + int index=tem.lastIndexOf("人民币"); + if(tem.contains("美元")) { + USDollarExchange.add(tem.substring(index+3,tem.length())); + // System.out.println(tem); + } + else if(tem.contains("欧元")) { + EuropeDollarExchange.add(tem.substring(index+3,tem.length())); + // System.out.println(tem); + } + else if(tem.contains("港元")) { + HongKongDollarExchange.add(tem.substring(index+3,tem.length())); + //System.out.println(tem); + } + } + } + catch(Exception e) + { + System.out.println("error"); + continue; + } + } +// System.out.println("共:"+USDollarExchange.size()+"条数据"); +// for (Iterator ite=USDollarExchange.iterator();ite.hasNext();) +// { +// System.out.println(ite.next()); +// } + // String[] array=USDollarExchange.toArray(new String[0]); + // System.out.println("美元中间值是:"+); + Label label=new Label(0,0,"人民币对美元汇率"); + sheet.addCell(label); + label=new Label(0,1,binarySearch(USDollarExchange.toArray(new String[0]),0,29,14)); + sheet.addCell(label); + label=new Label(1,0,"人民币对港币汇率"); + sheet.addCell(label); + label=new Label(1,1,binarySearch(HongKongDollarExchange.toArray(new String[0]),0,29,14)); + sheet.addCell(label); + label=new Label(2,0,"人民币对欧元汇率"); + sheet.addCell(label); + label=new Label(2,1,binarySearch(EuropeDollarExchange.toArray(new String[0]),0,29,14)); + sheet.addCell(label); + xls.write(); + xls.close(); + } + catch(Exception e) + { + System.out.println("error"+e.getMessage()); + System.exit(0); + } + } + private void getUrlDataList(int data_url_num) //获取二级url. data_url_num所需二级url数目. + { + getLinkUrlList(30); + Parser parser=new Parser(); + WebClient webclient=new WebClient(); + webclient.getOptions().setJavaScriptEnabled(true); + webclient.getOptions().setCssEnabled(true); + for(Iterator ite= link_url_list.iterator();ite.hasNext();) + { + try + { + String tt=ite.next(); + System.out.println("正在parser "+tt); + parser.setEncoding("utf-8"); + HtmlPage page=webclient.getPage(tt); + List anchors=page.getAnchors(); + int flag=0; + for (Iterator iterator=anchors.iterator();iterator.hasNext();) + { + String temp=iterator.next().getHrefAttribute(); + if (temp.matches("/zhengcehuobisi/125207/125217/125925/[0-9]{7}/index.html")) + { + flag++; + if(1==flag) + continue; //匹配到的第一个URL是无效的. + data_url_list.add("http://www.pbc.gov.cn" + temp); + System.out.println(temp); + } + } + } + catch(Exception e) + { + System.out.println("在parsser fetchURL时发生错误:"); + System.exit(0); + } + + } + + } + private void getLinkUrlList(int data_url_num) //获取一级url  data_url_num所需二级url数目. + { + int url_fetch_num=0; + if(data_url_num %26==0) + url_fetch_num= data_url_num /this.url_num_perpage; + else + url_fetch_num= data_url_num /this.url_num_perpage+1; + for (int i=1;i ite= link_url_list.iterator();ite.hasNext(); ) + { + System.out.println(ite.next()); + } + } + String binarySearch(String[] list , int start , int tail , int destination) //在list中寻求排序位于destination位置的值 + { + System.out.println("running"); + int index_head=start; + int index_tail=tail; + String tem=list[start]; + while(index_head=0) + { + list[index_tail]=list[index_head]; + index_tail--; + break; + } + } + } + list[index_head]=tem; + if(index_head==destination) + return list[index_head]; + else if(index_head