×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Aug 2009
    Location
    Iran - Tabriz
    Posts
    111

    read line by line from file

    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();  
    #   
    #     }  
    # }

  2. #2
    Regular Contributor
    Join Date
    Oct 2007
    Location
    Pune
    Posts
    189

    Re: read line by line from file

    What about using int read() until you found a CR (0x0D) or LN (0x0A)? If it was a CR, check whether the next character is a LN.
    Regards,
    Dinanath


    It's a matter of attitude.

  3. #3
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: read line by line from file

    There is no line-by-line reading for text files in the MIDP API. One option is to read the entire file into one long String (as you are doing), then to split it into lines. For example, use indexOf('\n') to find line breaks. You'll need to know whether your line breaks are '\n' or "\r\n".

    You might find it slightly faster to read the file into a byte[], then convert it to a String as new String(byteArray, "UTF-8"), it should be slightly faster. Check here for techniques for reading from JAR resource files.

    For including text files as resources in your JAR, you might like to look at this technique, which can be used for text localization, or for other text resources.

    Graham.

Similar Threads

  1. Problem with pyobfuscate
    By JOM in forum Symbian
    Replies: 3
    Last Post: 2008-06-20, 22:47
  2. Error while making sis file
    By lalitjadiya in forum Symbian
    Replies: 9
    Last Post: 2008-05-20, 17:18
  3. read file line by line
    By maciejprzybycien in forum Symbian
    Replies: 10
    Last Post: 2008-04-10, 14:14
  4. ARMI - THUMB
    By adrianogalati in forum Symbian
    Replies: 6
    Last Post: 2007-10-23, 19:57
  5. Replies: 8
    Last Post: 2007-10-16, 15:05

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×