• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Input data from a txt file?

 
Huang Qingyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear experts,
May I know how to read data from a text file(abc.txt) and print it inside a TextBox on a MIDlet?
I have try DataOInputStream and FileInputStream and there is always error.
Can you guys please kindly teach me how to do it?
Thanks alot, Franco
 
john muchow
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a simple block of code to read a file
InputStream is = getClass().getResourceAsStream("help.txt");
try
{
StringBuffer sb = new StringBuffer();
int chr, i = 0;
// Read until the end of the stream
while ((chr = is.read()) != -1)
sb.append((char) chr);
return sb.toString();
}
catch (Exception e)
{
System.out.println("Error");
}
In order for this to work, the file ("help.txt") will need to be packaged with the MIDlet in the JAR file.
John
Core J2ME Technology and MIDP
[ January 18, 2002: Message edited by: john muchow ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic