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

How to load external text file into a Form?

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I made a menu with 1-5 in a Form and 5 external text files. I want the user who is able to view the text content of the text file when he chooses one of them from the menu. And the text file screen has a "Back" command button to let him go back.
How can I do it and can it support other languages such as Chinese and Japanese?
Thanks for help.
Andrew
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I made the mistake about the subject, it should be loading local file, not external file through http.
I wrote a method but it throwed an exception when the midlet was run.
private void loadText()
{
try {
InputStream is = this.getClass().getResourceAsStream("/text.txt");
StringBuffer sb = new StringBuffer();
int chr;
while ((chr = is.read()) != -1)
{
sb.append((char) chr);
is.close();
}
}
catch (Exception e)
{
System.out.println("Error occurs while reading file");
}
}
I put the text.txt file in the same folder with the main file (extends midlet). How can I load the text content and display it on StringItem?
Thanks for any help.
Andrew
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that this.getClass().getResourceAsStream("/text.txt") does not accept a local source.
I can read the remote file with (InputConnection)Connector.open("http://www.domain.com/text.txt");
Anyone knows how should I read a local file?
Cheers
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"/text.txt" means that the "text.txt" file must be packed under the root of the JAR file (NOT in the MIDlet directory).
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the text file is located in the root dir of jar file.
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I changed the code and it is now:
public void loadText() throws IOException{
InputStream is = this.getClass().getResourceAsStream("/text.txt");
StringBuffer sb = new StringBuffer();
int chr, i = 0;
while ((chr = is.read()) != -1){
sb.append((char) chr);
}
is.close();
}
However, it throws a NullPointerException when I execute it.
Any idea?
Thanks for help.
Andrew
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The method works now and it does not throw the NullPointerException. May I ask you how should I display the text content of the text.txt file with a corresponding method?
Is there any sample code?
Thank you very much.
Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic