• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help in reading a file

Huang Qingyan
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
public void startApp() throws MIDletStateChangeException{
public void pauseApp() {
public void destroyApp(boolean unconditional) {
public void commandAction(Command c, Displayable s){
if(c == exitCommand){
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;
// Read until the end of the stream
while ((chr = is.read()) != -1)
sb.append((char) chr);
catch (Exception e){
System.out.println("Error occurs while reading file");
Please kindly give me your advice on this.
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.
Author Core J2ME Technology and MIDP
[ January 24, 2002: Message edited by: john muchow ]
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!