手机游戏中读取中文字符串文件的封装类

开发手机RPG游戏时,通常都有很多的对白,一般我们都是将对白保存在资源文件中,然后在程序中读取。读取西文字符比较简单,然而如果读取中文字符则稍微麻烦一点。下面是一个支持中文字符串读取的类,要注意:资源文件要保存为Unicode格式(记事本中文件->另存为对话框中即可选择保存的编码格式)。
源代码如下:
 
import java.io.*;
import java.util.Vector;
 
/**
 * ChineaseReader:读取Unicode编码格式保存的文件,支持中文字符串的读取。
 * @author 飘飘白云
 *
 */
public class ChineaseReader
{
 
       private String path ="/Unicode_CN_String.txt";
       private boolean beReaded = false; //读取文件标志
      
       private InputStream reader;
       private Vector vEnterPosition;       //存放换行(硬回车)的位置
       private Vector vText;      //存放各行的文本的vector
 
       /**
        * 设置要被读取文件的路径和文件名:"path/fileName"
        * @param path
        */
       public void setPath(String path)
       {
              this.path = path;
              beReaded = false;
       }
      
       /**
        * 取得指定行的字符串,行号从1开始。
        * @param line      行数
        * @return 返回指定行的字符串
        */
       public String getLine(int line)
       {
              if( !beReaded )
                     readFile();
                    
              if((line-1 > vText.size()) || (line-1) < 0 )
                     return null;
             
              return (String)vText.elementAt(line-1);
       }
      
       /**
        * 取得指定行中从tag标志开始的字符串,行号从1开始。
        * @param line      行号
        * @param tag      标记(<tag>)
        * @return
        */
       public String getLine(int line,String tag)
       {
              if(!beReaded)
                     readFile();
             
              String ret = new String();
              if((line-1 > vText.size()) || (line-1) < 0)
                     ret = null;
              else {
                     ret = (String)vText.elementAt(line-1);
      
                     String tagstr = "<"+tag+">";
                     int pos = ret.indexOf(tagstr);
                     if(pos == -1 ){
                            System.out.println("class ChineseReader can not read <"+tag+">.");
                            ret = null;
                     }
                     else{
                            pos = ret.indexOf(">");
                            ret = ret.substring(pos+1);
                     }
              }
              return ret;      
       }
      
       /**
        * 按行读取数据,保存行信息。
        */
       private void readFile()
       {
              if(beReaded)
                     return;
              try{
                     reader = Class.forName("java.lang.Object").getResourceAsStream(path);
              }
              catch(Exception e)
              {}
             
              vEnterPosition = new Vector(0,1);
              vText = new Vector(0,1);
             
              try
              {
                     byte[] bytesRead = new byte[4096*2];
                     int readlength = 0;
                     int c1, c2;
                     String sText = null;
 
                     reader.skip(2) ;       //FFFE
                     if ((readlength = reader.read(bytesRead)) > 0)           
                     {
                            char outputChars[] = new char[readlength/2];           
                            int j = 0; //outputChars数组中的游标
                            for (int i = 0; i < readlength && j < readlength/2 ; i = i+2,j++)
                            {
                                   c1 = (int)bytesRead[i];
                                   c2 = (int)bytesRead[i + 1];
                                  
                                   //换行符号:"/n", 字符    十进制    十六进制
                                   //                             '/'           92          5c
                                   //                             'n'          110        6e
                                   if(( i+3 < readlength )
                                                 && ( bytesRead[i] == 92 ) && ( bytesRead[i+1] == 0 )
                                                 && ( bytesRead[i+2] == 110 ) && bytesRead[i+3] == 0)
                                   {
                                          outputChars[j] = '/n';
                                          i = i+2;
                                          continue;
                                   }
                                  
                                   //回车换行:"/n",   字符    十进制    十六进制
                                   //                               回车      13          0d
                                   //                               换行      10          0a
                                   if( (i+3 < readlength)
                                                 && (bytesRead[i] == 0x0d ) && ( bytesRead[i+1] == 0 )
                                                 && ( bytesRead[i+2] == 0x0a ) && ( bytesRead[i+3] ==0 ))
                                   {
                                          vEnterPosition.addElement(new Integer(j));
                                   }
                                  
                                   if (c1 < 0 ) c1 += 256;
                                   if (c2 < 0 ) c2 += 256;
 
                                   outputChars[j] = (char)(c2 << 8 +c1);
                            }
                            sText = new String(outputChars);
                            sText = sText.trim();
                     }
                     else
                            sText = "";
                    
                     int num = vEnterPosition.size()+1;       //回车数加上最后的文本末位符
                     int startPos = 0,endPos = 0; //起点,终点
 
                     for(int i = 0; i < num-1; i++)
                     {
                            endPos = ((Integer)vEnterPosition.elementAt(i)).intValue();
                            vText.addElement(sText.substring(startPos,endPos));
                            startPos = endPos;
                     }
                    
                     vText.addElement(sText.substring(startPos));
                                  
                     beReaded = true;
                     //System.out.println(sText);
                     //for(i=0;i<vText.size();i++)
                     //     System.out.println(i+" is "+((String)vText.elementAt(i)));
              }
              catch(Exception e){
                     beReaded = false;
                     e.printStackTrace();
              }
       }
      
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值