• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to read a text file

 
Jiong Wang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven�t found a way to make the MIDlet read a text file. It seems that the CLDC library doesn't have the java.io.File class. Any other opinion?
 
William Frantz
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MIDlets run in a "sandbox" which means they don't (generally) have access to any local files.

You can store text files in the J2ME Record Management System (RMS) and read them from there.

You can retrieve text files from a server on the internet and display them.

You (generally) can't read anything created by another MIDlet or by any native phone application.

William Frantz
http://SprintDevelopers.com
[ February 15, 2005: Message edited by: William Frantz ]
 
Jignesh Kakkad
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the code from somewhere.

public void showFile() {
try {
FileConnection fc = (FileConnection)Connector.open("file:///C:/TEST.TXT");
if (!fc.exists()) {
throw new IOException("File does not exists");
}
InputStream fis = fc.openInputStream();
byte[] b = new byte[1024];

int length = fis.read(b, 0, 1024);

fis.close();
fc.close();

TextBox viewer = new TextBox(filePath, null, 1024,TextField.ANY | TextField.UNEDITABLE);

if (length > 0) {
viewer.setString(new String(b, 0, length));
}
Display.getDisplay(this).setCurrent(viewer);
}
catch (Exception e) {
e.printStackTrace();
}
}


But i could not able to find jar file of this
May i know from where i can find
even i alos want to do that.


Please let me know as soon as possible.


Thanx
 
Ken Loh
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last time I checked, you need a device that has Optional Packages to use FileConnection.

Originally posted by Jignesh Kakkad:
i got the code from somewhere.

public void showFile() {
try {
FileConnection fc = (FileConnection)Connector.open("file:///C:/TEST.TXT");
if (!fc.exists()) {
throw new IOException("File does not exists");
}
InputStream fis = fc.openInputStream();
byte[] b = new byte[1024];

int length = fis.read(b, 0, 1024);

fis.close();
fc.close();

TextBox viewer = new TextBox(filePath, null, 1024,TextField.ANY | TextField.UNEDITABLE);

if (length > 0) {
viewer.setString(new String(b, 0, length));
}
Display.getDisplay(this).setCurrent(viewer);
}
catch (Exception e) {
e.printStackTrace();
}
}


But i could not able to find jar file of this
May i know from where i can find
even i alos want to do that.


Please let me know as soon as possible.


Thanx
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic