Posted By:
Huang_Qingyan
Posted On:
Wednesday, January 23, 2002 08:28 AM
Hi, Im been trying to read a text file from my local drive using J2ME and I failed after a few days and so can you guys please kindly help me out? My codes goes like this: ---------------------------------------- package InputData; import java.io.*; import java.util.*; import javax.microedition.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class InputData extends MIDlet implements CommandListener{ private Command exitCommand, nextCommand; private Display display; private Form screen; private StringItem displayTitle; public InputD
More>>
Hi,
Im been trying to read a text file from my local drive using J2ME and I failed after a few days and so can you guys please kindly help me out?
My codes goes like this:
----------------------------------------
package InputData;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class InputData extends MIDlet implements CommandListener{
private Command exitCommand, nextCommand;
private Display display;
private Form screen;
private StringItem displayTitle;
public InputData(){
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 2 );
nextCommand = new Command("Next", Command.OK, 1);
screen = new Form("Data display here...");//Title on top of midlet
displayTitle = new StringItem("", "Default text");
//Default text shown
screen.append(displayTitle);
screen.addCommand(exitCommand);
screen.addCommand(nextCommand);
screen.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException{
//display.setCurrent(screen);
readData();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s){
if(c == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
public void readData(){
try {
String fileName = "file:C:/test.txt";
InputConnection conn = (InputConnection)Connector.open(fileName, Connector.READ);
InputStream is = conn.openInputStream();
StringBuffer sb = new StringBuffer();
int chr, i = 0;
conn.close();
// Read until the end of the stream
while ((chr = is.read()) != -1)
sb.append((char) chr);
is.close();
}
catch (Exception e){
System.out.println("Error occurs while reading file");
}
}
}
Please kindly give me your advice on this.
Thanks,
Franco
<<Less