Hi , how can i read a file line by line ???
i want to save each line in slot of String array
i write this code but it`s return all of file !!!
please help me to change this code
thanks
Code:# import java.io.*; # import javax.microedition.midlet.*; # import javax.microedition.lcdui.*; # # public class Midlet extends MIDlet implements CommandListener { # # private Display display; # private Form form; # private Command read, exit; # private Alert alert; # private String[] db; # private int i = 0; # # public Midlet() { # display = Display.getDisplay(this); # read = new Command("Read", Command.SCREEN, 1); # exit = new Command("Exit", Command.EXIT, 1); # form = new Form("Read File"); # form.addCommand(exit); # form.addCommand(read); # form.setCommandListener(this); # } # # public void startApp() { # display.setCurrent(form); # } # # public void pauseApp() { # } # # public void destroyApp(boolean unconditional) { # notifyDestroyed(); # } # # public void commandAction(Command c, Displayable s) { # String label = c.getLabel(); # if (label.equals("Read")) { # String stText = readUnicodeFile("test.txt", "UTF-8"); # if (stText != null) { # alert = new Alert("Reading", stText, null, null); # alert.setTimeout(Alert.FOREVER); # display.setCurrent(alert, form); # } # } else if (label.equals("Exit")) { # destroyApp(false); # } # } # # /* private String file(){ # InputStream is = getClass().getResourceAsStream("test.txt"); # StringBuffer sb = new StringBuffer(); # try{ # int chars, i = 0; # while ((chars = is.read()) != -1){ # sb.append((char) chars); # } # String stText=readUnicodeFile("test.txt","UTF-8"); # return sb.toString(); # }catch (Exception e){} # return null; # }*/ # private String readUnicodeFile(String string, String string0) { # StringBuffer buffer = new StringBuffer(); # # java.io.InputStream is = null; # java.io.InputStreamReader isr = null; # try { # int ch = 0; # Class c = this.getClass(); # is = c.getResourceAsStream(string); # isr = new InputStreamReader(is, string0); # # # while ((ch = isr.read()) > -1) { # buffer.append((char) ch); # } # isr.close(); # if (is != null) { # # is.close(); # } # # # } catch (Exception ex) { # ex.printStackTrace(); # } # return buffer.toString(); # # } # }


Reply With Quote

