在j2me中讀取txt文件數據

private void read()

{

  byte[] data = null;

  InputStream in=null;

  DataInputStream dis = null;

  int count=0;//文本文件 中字節的個數

  int highPostion=-1,lowPostion=-1;//分別代表一個數字的十位數字和各位數字。

  int num=0;//所得到的數字

  int numCount=0;

  try

  {

   in=this.getClass().getResourceAsStream("/help.txt");//將位於res目錄下的help.txt中數字讀出。

   dis = new DataInputStream(in);

   dis.mark(0);

   while(true)

   {

    if(dis.read()!=-1)

    {

     count++;

    }

    else

    {

     break;

    }

   }

   dis.reset();

   data = new byte[count];

   for(int i=0;i<count;i++)

   {

    data[i]=dis.readByte();

   }

   for(int i=0;i<data.length;i++)

   {

    if(data[i]!=44&&data[i]!=10&&data[i]!=13)

    {

     if(highPostion==-1)

     {

      highPostion=data[i]-48;

     }

     else

     {

      lowPostion=data[i]-48;

     }

    }

    else

    {

     if(data[i]==44)

     {

      numCount++;

      if(lowPostion!=-1&&highPostion!=-1)

      {

       num=highPostion*10+lowPostion;

      }

      else if(highPostion!=-1&&lowPostion==-1)

      {

       num=highPostion;

      }

      highPostion=-1;

      lowPostion=-1;

      System.out.println("/t"+i+"/t/t"+num+"/t/t"+numCount);

     }

    }

 

 

   }

  }catch(Exception e)

  {

   e.printStackTrace();

  }

  finally

  {

   in = null;

  }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章