J2ME游戏中读入文本并存储在String数组中

作者:网络 来源:佚名 更新时间:2008-07-28 22:01:37 点击:

一个在j2se中十分钟即告完成的代码,在j2me中花费了一个下午.是什么样的代码呢?
[要求]在游戏中读入文本形式存储的hints

[分析]
j2me没有bufferreader,用inputstreamreader吧
既然没有bufferreader,也就不要奢望有readline()了,自己慢慢循环吧

[源代码]
[code]
/* read hint-------------------------------------- */
/**
* this is the hints
* */
string[][] hintstr;

/**
* this specify the hint's show width
*/
final int hint_width = 8;

int hint_num;

public boolean readhint(int stage) {

if (stage > 0 && stage < 4) {
if (stage == 2) {
hint_num = 4;
} else {
hint_num = 3;
}
} else {
system.out.println("no this stage");
return false;
}

try {

string filename = "hint" + integer.tostring(stage);
inputstreamreader in = new inputstreamreader(getclass()
.getresourceasstream(filename));
int num = -1;
do {
++num;
} while (in.read() != -1);
in.reset();
char[] hintsdat = new char[num];
system.out.println(num);
in.read(hintsdat);
in.close();

string hint_temp = new string(hintsdat);

int j = 0;
int i = 0;
string hint_num_temp[] = new string[hint_num];
for (int x = 0; x < hint_num; x++)
hint_num_temp[x] = "";
while (true) {

if (i < hintsdat.length) {
if (hintsdat[i] != '\n') {
hint_num_temp[j] += hintsdat[i];
i++;
continue;
} else if (j < hint_num) {
system.out.println(i);
i++;
j++;
}
continue;
}

break;
}

// system.out.println("creating....");

j = 0;
for (i = 0; i < hint_num; i++) {
string str = new string(hint_num_temp[i]);
int str_num = str.length() / hint_width + 1;
hintstr = new string[num][str_num];
for (int z = 0; z < str_num; z++) {
if (z < str_num - 1) {
hintstr[j][z] = "";
hintstr[j][z] += str.substring(z * hint_width, (z + 1)
* hint_width);
} else {
hintstr[j][z] = "";
hintstr[j][z] += str.substring((z - 1) * hint_width);
}
system.out.println("hintstr[" + j + "][" + z + "]:"
+ hintstr[j][z]);
}
j++;
}

} catch (ioexception e) {
system.out.println("error");
}
return true;
}
[/code]
[总结]
程序要求很简单,一定有更好的办法来实现,结果参阅了大量资料,一无所获.