Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help in reading a file

 
Huang Qingyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
john muchow
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Files cannot be read from a hard-drive. The only means to open/read a file is if it is packaged inside the JAR file along with the MIDlet.
John
Author Core J2ME Technology and MIDP
[ January 24, 2002: Message edited by: john muchow ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic