Read Arabic file unicode in J2ME
Ni source kode nampilin isi dari file unicode (arabic, UTF-8) dalam J2ME.
class ReadFileText
class ReadFile
Pembacaan file dilakukan pada kelas ReadFile, dimana InputStream is = getClass().getResourceAsStream("help.utx");
class ReadFileText
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ReadFileText extends MIDlet {
Display display;
ReadFile read;
public ReadFileText() {
display = Display.getDisplay(this);
}
public void startApp() {
read = new ReadFile(this, display);
display.setCurrent(read);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void exitMIDlet() {
destroyApp(false);
notifyDestroyed();
}
}
class ReadFile
import javax.microedition.lcdui.*;
import java.io.*;
public class ReadFile extends Canvas implements CommandListener {
private ReadFileText midlet;
private Display display;
private Command cmExit;
private String help;
private StringItem si;
public ReadFile(ReadFileText midlet, Display display) {
//super("Read File Text");
this.midlet = midlet;
this.display = display;
if ((help = readHelpText()) != null) {
si = new StringItem("", help);
} else
si = new StringItem("Error", "File Help Tidak Ditemukan");
cmExit = new Command("Keluar", Command.EXIT, 1);
setCommandListener(this);
addCommand(cmExit);
}
public void commandAction(Command c, Displayable s) {
if (c == cmExit)
midlet.exitMIDlet();
}
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
//Warnai Background
g.setColor(0xffffff);
g.fillRect(0, 0, w, h);
g.setColor(0x000000);
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN, Font.SIZE_LARGE));
g.drawString(help, 20,20, Graphics.TOP | Graphics.HCENTER);
}
private String readHelpText() {
StringBuffer sb = new StringBuffer();
InputStreamReader isr = null;
try {
InputStream is = getClass().getResourceAsStream("help.utx");
if (is == null) throw new Exception("File gak ada!");
isr = new InputStreamReader(is,"UTF-16");
sb = new StringBuffer();
int chr, i = 0;
while ((chr = isr.read()) != -1)
sb.append((char) chr);
return sb.toString();
} catch (Exception e) {
}
return null;
}
};
Pembacaan file dilakukan pada kelas ReadFile, dimana InputStream is = getClass().getResourceAsStream("help.utx");
kalau untuk karakter huruf jepang gimana? pakai itu juga bisa atau tidak? mohon bantuannya yah,,saya sedang membangun kamus bahasa jepang dengan j2me,,
ReplyDeletebisa.. tinggal save data filenya,(bisa pake notepad).. jangan lupa untuk save-as encodingnya..
ReplyDelete