lyngsat網站衛星資料解析

來源:互聯網
上載者:User

標籤:

 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());  } }}

lyngsat網站衛星資料解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.