use*_*521 1 java java-me java-micro-editon-sdk3.0
我在我的应用程序中使用了一些阿拉伯语文本.在模拟器上阿拉伯语文本是diplaying罚款.
但是在设备上它没有正确显示.
在模拟器上它就像مرحبا.
但在设备上它就像مرحبا.
我需要的是这个مرحبا.
为MIDP应用程序创建文本资源,以及如何在运行时加载它们.这种技术是unicode安全的,因此适用于所有语言.运行时代码小,速度快,使用的内存相对较少.
创建文本源
??????????? ??????? ?????????? ???????????????????????????? ????????
?????????
Run Code Online (Sandbox Code Playgroud)
该过程从创建文本文件开始.加载文件后,每一行都成为一个单独的String对象,因此您可以创建一个文件,如:
这需要采用UTF-8格式.在Windows上,您可以在记事本中创建UTF-8文件.确保使用"另存为...",然后选择"UTF-8编码".

将名称命名为arb.utf8
这需要转换为MIDP应用程序可以轻松读取的格式.MIDP不提供读取文本文件的便捷方法,如J2SE的BufferedReader.在字节和字符之间进行转换时,Unicode支持也可能是一个问题.读取文本的最简单方法是使用DataInput.readUTF().但要使用它,我们需要使用DataOutput.writeUTF()编写文本.
下面是一个简单的J2SE命令行程序,它将读取您从记事本中保存的.uft8文件,并创建一个.res文件以进入JAR.
import java.io.*;
import java.util.*;
public class TextConverter {
public static void main(String[] args) {
if (args.length == 1) {
String language = args[0];
List<String> text = new Vector<String>();
try {
// read text from Notepad UTF-8 file
InputStream in = new FileInputStream(language + ".utf8");
try {
BufferedReader bufin = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String s;
while ( (s = bufin.readLine()) != null ) {
// remove formatting character added by Notepad
s = s.replaceAll("\ufffe", "");
text.add(s);
}
} finally {
in.close();
}
// write it for easy reading in J2ME
OutputStream out = new FileOutputStream(language + ".res");
DataOutputStream dout = new DataOutputStream(out);
try {
// first item is the number of strings
dout.writeShort(text.size());
// then the string themselves
for (String s: text) {
dout.writeUTF(s);
}
} finally {
dout.close();
}
} catch (Exception e) {
System.err.println("TextConverter: " + e);
}
} else {
System.err.println("syntax: TextConverter <language-code>");
}
}
}
Run Code Online (Sandbox Code Playgroud)
要将arb.utf8转换为arb.res,请将转换器运行为:
java TextConverter arb
Run Code Online (Sandbox Code Playgroud)
在运行时使用Text
将.res文件放在JAR中.
在MIDP应用程序中,可以使用以下方法读取文本:
public String[] loadText(String resName) throws IOException {
String[] text;
InputStream in = getClass().getResourceAsStream(resName);
try {
DataInputStream din = new DataInputStream(in);
int size = din.readShort();
text = new String[size];
for (int i = 0; i < size; i++) {
text[i] = din.readUTF();
}
} finally {
in.close();
}
return text;
}
Run Code Online (Sandbox Code Playgroud)
加载和使用这样的文本:
String[] text = loadText("arb.res");
System.out.println("my arabic word from arb.res file ::"+text[0]+" second from arb.res file ::"+text[1]);
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.谢谢
| 归档时间: |
|
| 查看次数: |
748 次 |
| 最近记录: |