package com.wh.sattp; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.OutputStreamWriter; public class Dealhtml { private static String filepath=""; private static String filepath_common=""; private static String filepath_android=""; /*處理的文件夾。在D:/satdeal/下根據地區建立文件夾,每一個文件夾 下面 分爲 txt common android 三個子文件夾 txt中的數據爲網站的源代碼保存的txt文本數據。common爲解析出的通用數據*/ //private static String FilePath="D:/satdeal/atlantic/"; //private static String FilePath="D:/satdeal/europe/"; //private static String FilePath="D:/satdeal/asia/"; private static String FilePath="D:/satdeal/test/"; public static void main(String[] args) { //列出當前文件夾裏面的內容,刪除掉common和android裏面的文件 File file=new File(FilePath+"txt"); File files[]=file.listFiles(); int size=files.length; filepath_common=FilePath+"/common/"; filepath_android=FilePath+"/android/"; System.out.println(System.currentTimeMillis()); fileinit(); for(int i=0;i<size;i++) { filepath=files[i].getAbsolutePath(); filepath_common=FilePath+"/common/"; filepath_android=FilePath+"/android/"; //System.out.println("str==" + filepath); dealfile(filepath); } System.out.println(System.currentTimeMillis()); } /* * 刪除掉文件夾中原有的文件 * */ private static void fileinit() { File file=new File(filepath_common); File files[]=file.listFiles(); int csize=files.length; for(int i=0;i<csize;i++) { files[i].delete(); } file=new File(filepath_android); files=file.listFiles(); csize=files.length; for(int i=0;i<csize;i++) { files[i].delete(); } } /* * 拿到指定的txt文本 開始解析數據 * 1.解析<title> 裏面包含衛星名及經度, * 2.肯定common 和android下面的文件名 * */ private static void dealfile(String path) { File file = new File(path); FileInputStream ins; try { ins = new FileInputStream(file); int readsize; byte b[] = new byte[1024000]; String str=null; while ((readsize = ins.read(b)) >= 0) { System.out.println("readsize=====" + readsize); byte first[]=new byte[100]; for(int g=0;g<100;g++) { first[g]=b[g]; } String fis=new String(first); int index1=fis.indexOf("<title>"); int index2=fis.indexOf("</title>"); String filename=fis.substring(index1+7, index2).replace('\\',' '); filename=filename.replace('/', ' '); filepath_common+=filename+".txt"; filepath_android+=filename+".xml"; System.out.println("filename=====" + filename); //<title>Thor 5 at 0.8°W - LyngSat</title> str += new String(Byte_dealto_String(b,readsize)); //System.out.println("str=====" + str); } dealstr(str); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * 處理傳入的byte數組 * 規則:1.先取出全部的 HVRL字符; * 2.原樣過濾掉前五個和最後五個數據 實際傳入的數據是要<1024000的 * 3.判斷連續的四個數字或者五個數字(頻率和符號率都是4位或者5位的) * freq的兩種格式: >123456& 和> 1234& * symb的兩種格式: >12345- 和>1234- * 取出用count標示: * 小於4時跳過相同個數 * count==10 4位freq * count==11 4位symb * count==20 5位freq * count==21 5位symb * 4.freq 用<>包裹起來 symb 用* #並轉行包裹 返回有效byte數組 * * */ private static byte[] Byte_dealto_String(byte[] b,int size) { byte c[] = new byte[10240]; int j = 0; //int size = b.length; //System.out.println("size===="+size); for (int i = 0; i < size; i++) { int count=1; if((b[i]=='H'||b[i]=='V')||(b[i]=='R'||b[i]=='L')) { if(i-2>=0&&b[i-1]==';'&&b[i-2]=='p'&&b[i+1]=='<') { c[j] = b[i]; j++; } } if (i < 5 || i > size-5) { if (b[i] >= '0' && b[i] <= '9') { c[j] = b[i]; j++; } } else { if (b[i] >= '0' && b[i] <= '9') { count=1; if (b[i + 1] >= '0' && b[i + 1] <= '9') { count=2; if (b[i + 2] >= '0' && b[i + 2] <= '9') { count=3; if (b[i + 3] >= '0' && b[i + 3] <= '9') { count=4; if(b[i-1]=='>'||(b[i-2]=='>'&&b[i-1]==' ')) { if(b[i + 4] >= '0' && b[i + 4] <= '9') { count=5; if(b[i + 5] == '&') { count=20; }else if(b[i + 5] == '-') { count=21; } }else if(b[i + 4] == '&') { count=10; }else if(b[i + 4]=='-') { count=11; } } } } } } } if(count<4) { i+=count-1; }else { if(count>5&&count<20) { //freq if(count==10) { c[j]='<'; j++; }else if(count==11) { c[j]='*'; j++; } c[j]=b[i]; j++; i++; c[j]=b[i]; j++; i++; c[j]=b[i]; j++; i++; c[j]=b[i]; j++; i++; if(count==10) { c[j]='>'; j++; }else if(count==11) { c[j]='#'; j++; c[j]='\n'; j++; } } if(count>12) { //freq if(count==20) { c[j]='<'; j++; }else if(count==21) { c[j]='*'; j++; } c[j]=b[i]; j++; i++; c[j]=b[i]; j++; i++; c[j]=b[i]; j++; i++; c[j]=b[i]; j++; i++; c[j]=b[i]; j++; i++; if(count==20) { c[j]='>'; j++; }else if(count==21) { c[j]='#'; j++; c[j]='\n'; j++; } } } } c[j]=' '; j++; c[j]=' '; byte d[] = new byte[j]; int dsize=d.length; for(int k=0;k<j;k++) { d[k]=c[k]; } return d; } /* * 處理獲得的數據。 * 根據 \n來切出字符串數組 此時有效數據爲<12345>V*67890# * 解析數組 先找到 VHRL*的組合定位 < #的位置 * 規則 : 1.已VHRL爲起始 前6和前7 定位< 後6後7定位# * 2.按照定位切割有用數據 保存爲 <12345>V*67890 * 判斷檢測切割的數據 * 1.檢測 > 位置應該大於等於5且小於等於6 * 2.判斷<>中的數字 應該在3000-13000之間 * * * 解析完畢 開始存儲數據到指定文件夾 * */ private static void dealstr(String str) { String all[]=str.split("\n"); //System.out.println("str===" + all.length); int size=all.length; int count=0; String strnew[]=new String[200]; for(int i=0;i<size;i++) { byte bstr[]=all[i].getBytes(); int len= bstr.length; for(int j=0;j<len;j++) { if((bstr[j]=='R'||bstr[j]=='L'||bstr[j]=='H'||bstr[j]=='V')&&bstr[j+1]=='*') { int first=0; int end=0; if(j-7>=0&&bstr[j-7]=='<') { first=j-7; } if(j-6>=0&&bstr[j-6]=='<') { first=j-6; } if(j+7<len&&bstr[j+7]=='#') { end=j+7; } if(j+6<len&&bstr[j+6]=='#') { end=j+6; } String str_us=all[i].substring(first, end); //System.out.println("str_us==" + str_us); int freqindex=str_us.indexOf('>'); if(freqindex<=4) { System.out.println(" Error**** freqindex=" + freqindex+" str_us="+str_us); continue; } String strfreq=str_us.substring(1, freqindex); int freq=Integer.valueOf(strfreq); if(freq>13000||freq<3000) { System.out.println("Error**** freq==" + strfreq); //continue; }else{ strnew[count]=all[i].substring(first, end); count++; } break; } } } try { FileWriter fw = new FileWriter(filepath_common,true); String allstr=""; for(int i=0;i<count;i++) { int lens=strnew[i].length(); if(lens<11||lens>15) { System.out.println("Error:****"+filepath_common+"****"+strnew[i]); }else{ fw.write(strnew[i]+"\n",0,lens+1); } } fw.flush(); }catch(Exception e) { System.out.println(e.toString()); } String_toandroid_file(strnew,count); } /* * 將數據保存爲須要的android xml數據 * 數據原樣爲<12345>V*67890 * 1.先替換<> 數據變爲 <transponder frequency="12345000" symbol_rate="V*67890 * 2.在將字串切爲 <transponder frequency="12345000" symbol_rate="V 67890 * 3.在切爲 <transponder frequency="12345000" symbol_rate=" V * 4.拼湊 <transponder frequency="12345000" symbol_rate="67890000" * 5.最後<transponder frequency="12345000" symbol_rate="67890000" polarisation="V"/> * * */ private static void String_toandroid_file(String allstr[],int count) { int size=allstr.length; for(int i=0;i<count;i++) { //<10962>H*1550 String str=allstr[i]; str=str.replaceAll( "<","<transponder frequency=\""); str=str.replaceAll( ">","000\" symbol_rate=\""); String strw[]=new String[2]; int index=str.indexOf('*'); strw[0]=str.substring(0, index); strw[1]=str.substring(index+1, str.length()); int length=strw[0].length(); String strq=strw[0].substring(length-1, length); String stra=strw[0].substring(0,length-1); stra+=""+strw[1]+"000\""; if(strq.equals("H")||strq.equals("L")) { stra+= " polarisation=\"H\"/>"; }else { stra+= " polarisation=\"V\"/>"; } stra+="\n"; allstr[i]=stra; } try { FileWriter fw = new FileWriter(filepath_android,true); //String allstr=""; for(int i=0;i<count;i++) { fw.write(allstr[i],0,allstr[i].length()); } fw.flush(); }catch(Exception e) { System.out.println(e.toString()); } } }