Reading text file in J2ME

与世无争的帅哥 提交于 2019-12-01 08:35:43

The simplest way is to do the correct thing: Use a Reader to read text data:

String content = "";
Reader in = new InputStreamReader(this.getClass().getResourceAsStream("asdf.txt"), THE_ENCODING);
StringBuffer temp = new StringBuffer(1024);
char[] buffer = new char[1024];
int read;
while ((read=in.read(buffer, 0, buffer.len)) != -1) {
  temp.append(buffer, 0, read);
}
content = temp.toString().

Not that you definitely should define the encoding of the text file you want to read. In the example above that would be THE_ENCODING.

And note that both your code and this example code work equally well on Java SE and J2ME.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!