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]
# 1
please search in this forum it has been asked thousand times...
suparenoa at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

I have same question.

J2ME does not have BufferedReader.readLine(). I don't know is there good ways to read text line by line.

In my case, I wrote a mehod to readLine from an Reader :

String readLine(Reader r) throws Exception

{

int c = r.read();

if( c == -1 )

return null;

String s = "";

while(c!=-1 && c!= '\n' && c!= '\r')

{

s += (char) c;

c = r.read();

}

return s;

}

yixiaoqianga at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
InputStream inputStreamTxt =null;inputStreamTxt = this.getClass().getResourceAsStream("file.txt");StringBuffer buf = new StringBuffer();int c ; while ((c = inputStreamTxt.read()) != -1) {buf.append((char)c);}inputStreamTxt.close();
PeppeMEa at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4
Thank you PeppeME.This works very well. But I still have problem of reading line by line.
theotimea at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5
make an internal while that stop every line separator ("/r/n") and insert any line into a separated stringbuffer.
PeppeMEa at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
Hi PeppeME.OK. I am confused with what u are saying.Can u make it clear with codes.thanks
theotimea at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

Something like this.

InputStream inputStreamTxt =null;

inputStreamTxt = this.getClass().getResourceAsStream("file.txt");

StringBuffer buf = new StringBuffer();

Vector lines = new Vector();

int c ;

while ((c = inputStreamTxt.read()) != -1)

{

char ch = (char)c;

if (ch == '\n')

{ lines.addElement(buf.toString());

buf.delete(0,buf.lenght);

}

else

buf.append(ch);

}

inputStreamTxt.close();

PeppeMEa at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8

Thank you very much PeppeME.

Your code helped me so much.

But I have two problems,

The code reads perfectly the file and stores it in vector, but when I try to display the content of vector, it displays also some empty lines at the end.

I don't know why?

Other thing, When apply a sorting algorithms to sort the words in the vector it is refusing.

Can u help me to solve those problems.

Thank u

theotimea at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9
for empty lines you must add control for other newline escape code \r for example.About sorting algorithm I can't help you because I don't know about you sorting algorithm code and the relative error you have.
PeppeMEa at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10
ok thank u man.let me try what u are saying to solve the first problem. for the second I will tell u later.
theotimea at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 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;

}

}

theotimea at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 12

Please PeppeMe, can u show me with codes. I tried but I don't arrive at the right solution Thank u.

Something like this.

InputStream inputStreamTxt =null;

inputStreamTxt = this.getClass().getResourceAsStream("file.txt");

StringBuffer buf = new StringBuffer();

Vector lines = new Vector();

int c ;

while ((c = inputStreamTxt.read()) != -1)

{

char ch = (char)c;

if (ch == '\n')

{ lines.addElement(buf.toString());

buf.delete(0,buf.lenght);

}

else

buf.append(ch);

}

inputStreamTxt.close();

theotimea at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 13
If the problem is the two line at the end, don't append it to output.In you while ignore the endline escape character.
PeppeMEa at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 14

Thank u PeppeME.

it works.

I have other two questions.

First I would like to input Strings from keyboard without using the J2ME interface(the one which looks like a mobile phone). I don't want to use that I want to use COMPUTER keyboard. how can i do this?

Secondly, I would like to write messages as u write SMS in mobile phone. I want to use J2ME Kyepad. my question is to know how i can use the J2ME keypad. I want to implement a predictive text system

Thank u

theotimea at 2007-7-12 10:25:19 > top of Java-index,Java Mobility Forums,Java ME Technologies...