Java Tutorial/J2ME/File Stream

Материал из Java эксперт
Перейти к: навигация, поиск

Read file content to TextBox

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
public class TextBoxMIDlet extends MIDlet {
  private static final int MAX_TEXT_SIZE = 64;
  protected TextBox textBox;
  protected Display display;
  protected void startApp() {
    String str = null;
    try {
      InputStream is = getClass().getResourceAsStream("resources/text.txt");
      InputStreamReader r = new InputStreamReader(is);
      char[] buffer = new char[32];
      StringBuffer sb = new StringBuffer();
      int count;
      while ((count = r.read(buffer, 0, buffer.length)) > -1) {
        sb.append(buffer, 0, count);
      }
      str = sb.toString();
    } catch (IOException ex) {
      str = "Failed to load text";
    }
    textBox = new TextBox("TextBox Example", str, MAX_TEXT_SIZE, TextField.ANY);
    Ticker ticker = new Ticker("This is a ticker...");
    textBox.setTicker(ticker);
    display = Display.getDisplay(this);
    display.setCurrent(textBox);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}