手机游戏中读取中文字符串文件的封装类
发布日期:2021-07-18 07:42:30 浏览次数:27 分类:技术文章

本文共 3083 字,大约阅读时间需要 10 分钟。

开发手机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();
}
}
}

转载地址:https://blog.csdn.net/iteye_16215/article/details/81370358 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:基于Nokia平台的游戏通用框架
下一篇:字符串的分割方法

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年01月02日 17时13分09秒