read text files in J2ME
Hi All,
I would like to read a text file called "words.txt"
words in that file are arranged in notepad line by line like this:
good
hello
bye
ok
Some one can help me how I can read them in J2ME line by line so that it is easy to manipulate them.
thanks
[307 byte] By [
theotimea] at [2007-11-27 5:06:33]

# 11
Hi, PeppeME.
I tried what u told me to remove empty lines.
This my code, it takes a text file of words.
for example:
bye
hi
gone
hood
good
ok
After it takes a very word and convert it into its corresponding numeric signature similar to one of mobile phone.
for example good will give 4663.
After it diplays a string of word and signature
for good it will diplay 4663,good
My problem is that when I dispay the whole text files, it comes like this:
293,bye
44,hi
4663,gone
4663,hood
4663,good
65,ok
, //empty lines
,//empty line
My problem is to remove the two empty lines at the end.
See my code:
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.util.Vector;
import java.util.Enumeration;
public class ReadFile extends MIDlet {
private Vector dict;
public ReadFile()
{
dict = new Vector();
}
public void startApp() {
readfile();
}
public void destroyApp(boolean b) {}
public void pauseApp() {}
public Vector readfile()
{
try {
Class c = this.getClass();
InputStream is = c.getResourceAsStream("help.txt");
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = isr.read()) != -1)
{
char th = (char)ch;
if (th == '\n')
{
dict.addElement(new WordSig(toNumeric(buffer.toString()),buffer.toString()));
//dict.addElement(buffer.toString());
buffer.delete(0,buffer.length());
}//end if
else if ( th != '\r' ) {
buffer.append(th);
}
}//end while
isr.close();
}
catch(UnsupportedEncodingException e){
System.err.println(e);
}
catch (IOException e)
{
System.err.println(e);
}
for (Enumeration e = dict.elements() ; e.hasMoreElements() ;)
{
System.out.println(e.nextElement());
}
return dict;
}//end method readfile
public static String toNumeric (String word) {
String lowerWord = word.toLowerCase();
StringBuffer result = new StringBuffer("");
for (int i = 0; i < lowerWord.length(); i++) {
char c = lowerWord.charAt(i);
if ("abc".indexOf(c) > -1) result.append("2");
else if ("def".indexOf(c) > -1) result.append("3");
else if ("ghi".indexOf(c) > -1) result.append("4");
else if ("jkl".indexOf(c) > -1) result.append("5");
else if ("mno".indexOf(c) > -1) result.append("6");
else if ("pqrs".indexOf(c) > -1) result.append("7");
else if ("tuv".indexOf(c) > -1) result.append("8");
else if ("wxyz".indexOf(c) > -1) result.append("9");
else if (" ".indexOf(c) > -1) result.append("9");
else result.append("?");
}
return result.toString();
}//end toNumeric method
}//end ReadFile class
class WordSig implements Comparable {
String word;
String sig;
public WordSig (String sig, String word) {
this.sig = sig;
this.word = word;
}
public String getSig () {
return this.sig;
}
public String getWord() {
return this.word;
}
public int compareTo (Object ws) {
return sig.compareTo(((WordSig) ws).getSig());
}
public boolean equals (Object ws) {
return sig.equals(((WordSig) ws).getSig());
}
public String toString () {
return sig + ", " + word;
}
}