[JavaME] Textdatei zeilenweise einlesen.

SunboX

Mitglied
In JavaME gibt es ja keinen Befehl, um eine Textdatei zeilenweise (readLine) einzulesen. Wie kann ich dies anders lösen?
Ich habe schon probiert mit readUTF immer 40 char einzulesen und zu gucken, ob ein Zeilenumbruch darin vorkommt, wenn n icht wieder 40 u.s.w. Aber das hat alles nicht so wirklich funktioniert. Gibt es da schon einen Workaround?

thanx SunboX
 
Ich hab es mittlerweile selbst geschafft. Falls es jemand braucht, unten steht der Code (complettes midlet). Die Datei "geo_kurz.dat" muß im Projekt im "res" Verzeichnis liegen!

Code:
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class Test extends MIDlet {

  public void startApp() {
    String str = "null";
    try {
      Class c = this.getClass();
      InputStream is = c.getResourceAsStream("/geo_kurz.dat");
      InputStreamReader isr = new InputStreamReader(is);
      str = readLine(isr);
      System.out.println(str);
    } catch (IOException e) {
      e.printStackTrace();
    }
    Display display = Display.getDisplay(this);
    TextBox tb = new TextBox("test", str, str.length(), 0);
    display.setCurrent(tb);
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {
    notifyDestroyed();
  }

    /**
    * Read one line from the Reader. A line may be terminated
    * by a single CR or LF, or the pair CR LF.
    * @param aReader The Reader to read characters from.
    * @return Next physical line.
    * @throws IOException if anything goes wrong.
    */

   boolean CRState = false;

   protected String readLine(Reader aReader) throws IOException {
     StringBuffer sb = new StringBuffer();
     boolean done = false;
     while (!done) {
       int result = aReader.read();
       if (result == -1) {
         if (sb.length() > 0) {
           break;
         }
         return null; // also possible is: throw new EOFException();
       }
       else {
         char ch = (char) result;
         if (ch == '\n') { // LF
           if (CRState) {
             CRState = false;
             continue;
           }
           break;
         }
         else {
           if (ch == '\r') {
             CRState = true;
             break;
           }
           else {
             sb.append(ch);
             CRState = false;
           }
         }
       }
     }
     return sb.toString();
   }
}

Ciao SunboX
 
Zuletzt bearbeitet:
Zurück