关于读外部text文件,并自动换行显示。

efeng0973 2004-08-11 03:46:30
我在做一个养成类游戏,因为文字信息量比较大,所以用一个text文件记录文字,然后加到包里,请问怎样把中文字符读进来,放在一个数组里,然后显示在屏幕上,并自动换行。根据屏幕宽度来决定显示几个字符。
...全文
332 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyingghost 2004-08-16
  • 打赏
  • 举报
回复
楼上,是你的编码方式不对。电脑模拟器上一般默认是GBK,但手机上不是。甚至不一定支持GBK。最好用UTF-8。
goodnow 2004-08-16
  • 打赏
  • 举报
回复
底下是我自己写的读取文件中一行字符的方法,虽然能顺利读出第lineNumber行
但是还有一个问题没有解决:在模拟器上能正常显示出中文,
而在N7650上却显示乱码,希望能进一步改进

private String myReadLine (int lineNumber) {

String testFileName = "/test.txt"; //测试的文本文件名
ByteArrayOutputStream s;
InputStream in;

// 一行文本是以行结束标志(\n or \r)为终止的一行字符 一般是'\r'0D(13)后'\n'0A(10)

in = this.getClass().getResourceAsStream(testFileName);
s = new ByteArrayOutputStream();
try{
int ch=0;
int i=0;
while (i<lineNumber-1){ //跳过前面的n-1行
ch = in.read();
if (ch == 13) i++;
}

ch=in.read();
if ( ch ==10 || ch ==13) ch=in.read(); //若前面没有把换行符去除干净 则跳过一个字符
while ( ch !=13 && ch !=10 &&ch!=-1 ) {
s.write( ch );
ch=in.read();
}
in.close();
} catch (IOException ioe){
System.out.println(ioe.toString());
}
String str = s.toString();
try{
s.close();
} catch (IOException ioe){
System.out.println(ioe.toString());
}
return str.trim();
}
hesi726 2004-08-16
  • 打赏
  • 举报
回复
老兄啊,你这个问题时 编解码的转换问题啊!!
熟不熟悉UTF-8编码??
不熟悉的话,建议你上 google 找找看别人有没有编解码的类吧!!
或者看看,也很简单的!!一个中文的两个字节变成3个字节而已! ^_^
goodnow 2004-08-16
  • 打赏
  • 举报
回复
问题就在于文件中是ANSI方式编码的若干行中英文,
该如何读出并转换成UTF-8?
朋友别哭 2004-08-15
  • 打赏
  • 举报
回复
up
prok 2004-08-12
  • 打赏
  • 举报
回复
分别判断屏幕和字符串长度
zcwhgj 2004-08-11
  • 打赏
  • 举报
回复

这个例子是计算字符数及其宽度进行换行显示的东西.
private int appelement(String str, xx k1){
int nextBeginx = currentBeginx;
int strlength=str.length();
int endpos =0;
int strWidth=displayFont.stringWidth(str);
int subStringWidth=0;
//Integer hashIndex =new Integer(-1);
if(strlength>0){
if (strWidth >= displayWidth - currentBeginx) {
//需要进行载取
int slen =0;
do{
subStringWidth = displayFont.substringWidth(str, endpos, ++slen);
if (subStringWidth >= displayWidth-currentBeginx) {
if (k1 != null) {
k1.a(strList.size());
}
tuneCurStartY(displayFont.getHeight());
subStringWidth=displayFont.substringWidth(str,endpos, slen-1);
String s =str.substring(endpos, endpos+slen-1);
strList.addElement(new StringRegion(s,
currentBeginx,
getcurenBeginy(displayFont.getHeight(), true),
displayFont, colorType));
nextBeginx = 0;
endpos = endpos+slen-1;
slen = 0;
getNextBeginY(displayFont.getHeight());
}
else {
if (endpos+slen==strlength) {
if (k1 != null) {
k1.a(strList.size());
}
String s =str.substring(endpos, strlength);
strWidth = displayFont.stringWidth(s);
tuneCurStartY(displayFont.getHeight

strList.addElement(new StringRegion(s,
currentBeginx,
getcurenBeginy(displayFont.getHeight(), true),
displayFont, colorType));
currentBeginx =strWidth + currentBeginx;
nextBeginx = currentBeginx;
break;
//getNextBeginY(displayFont.getHeight());
}
}
}while(true);
}
else {
if (k1 != null) {
k1.a(strList.size());
}
tuneCurStartY(displayFont.getHeight());
strList.addElement(new StringRegion(str, currentBeginx,
getcurenBeginy(displayFont.getHeight(), true), displayFont,colorType));
currentBeginx += strWidth ;
nextBeginx=currentBeginx;
}
//重绘
}
if (strcanvas.blnScroll) {
strcanvas.repaint(0, 0, displayWidth, displayHeight);
}
return nextBeginx;
}

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧