×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Regular Contributor
    Join Date
    Mar 2006
    Posts
    76

    Question how to read text file in j2me

    Hi,
    I want to build an application in which i've to read text file.Can any one help me regarding this.Is there any API/method/site for reference to do so?(with actual example's source code.)

    Sachin Warang.

  2. #2
    Super Contributor
    Join Date
    Nov 2003
    Location
    Bangalore , India
    Posts
    4,430

    Re: how to read text file in j2me


  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Dhaka, Bangladesh
    Posts
    9

    Re: how to read text file in j2me

    Hi

    I develop an mobile application. which need to read text file.
    i am faceing one problem. when i run the application in my desktop. i have use the file path, file:///root1/myText.txt, it is working in my desktop..
    but when i run this application in mobile the application is not getting the path.. would you plz help me.... how i need to show the path.. like for mobile Nokia N97, or N70.. or any common path for all the mobiles

  4. #4
    Registered User
    Join Date
    Oct 2010
    Location
    Dhaka, Bangladesh
    Posts
    9

    Re: how to read text file in j2me

    Hi Sachin.

    I thing the following coul help you..

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

    public class ReadFile extends MIDlet implements CommandListener{
    private Display display;
    private Form form;
    private Command read, exit;
    private Alert alert;

    public ReadFile(){
    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 string = file();
    if (string != null){
    alert = new Alert("Reading", string, null, null);
    alert.setTimeout(Alert.FOREVER);
    display.setCurrent(alert, form);
    }
    } else if (label.equals("Exit")){
    destroyApp(false);
    }
    }

    private String file(){
    InputStream is = getClass().getResourceAsStream("help.txt");
    StringBuffer sb = new StringBuffer();
    try{
    int chars, i = 0;
    while ((chars = is.read()) != -1){
    sb.append((char) chars);
    }
    return sb.toString();
    }catch (Exception e){}
    return null;
    }
    }

  5. #5
    Super Contributor
    Join Date
    Oct 2008
    Location
    INDIA
    Posts
    2,326

    Re: how to read text file in j2me

    This should be used for path : file:///C:/Data/+

    Also see the File connection API document for reference. Search in the wiki Java .

    Best regards,

  6. #6
    Regular Contributor
    Join Date
    Dec 2008
    Location
    Mumbai
    Posts
    199

    Re: how to read text file in j2me

    hi frnd,

    your application will not work on mobile application if you hard coded root name of filesystem.In Nokia and sony phone you will find C: and EMemory card).and In samsung and LG also have different.so dont hard code value.first find root of mobile.by using FileSystemRegistry.listRoots().
    Ajay Prajapati
    Mobile Software Developer
    Mumbai,India

  7. #7
    Regular Contributor
    Join Date
    Apr 2009
    Location
    Kuala Lumpur
    Posts
    98

    Re: how to read text file in j2me

    This is my code to read an image. simply you can change it to read text message:

    PHP Code:
    public Image readFile(String path)//test===============
        
    {
            try
            {
                
    FileConnection fc = (FileConnection)Connector.open(pathConnector.READ);
                if(!
    fc.exists()) {
                    
    System.out.println("File doesn't exist!");
                }
                else
                {
                    
    int size = (int)fc.fileSize();
                    
    InputStream is fc.openInputStream();
                    
    byte bytes[] = new byte[size];
                    
    is.read(bytes0size);
                    
    image Image.createImage(bytes0size);
                }

            } catch (
    IOException ioe) {
                
    System.out.println("IOException: "+ioe.getMessage());
            } catch (
    IllegalArgumentException iae) {
                
    System.out.println("IllegalArgumentException: "+iae.getMessage());
            }
            
            return 
    image;
        } 

Similar Threads

  1. How to create text file by J2ME ?
    By Vinit410 in forum Mobile Java General
    Replies: 18
    Last Post: 2012-07-24, 05:36
  2. socket read hangs until socket is closed
    By camroe in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-07-21, 09:44
  3. How to read Video File
    By rasikasri in forum Symbian Media (Closed)
    Replies: 0
    Last Post: 2005-03-30, 13:01
  4. How to display text from a file
    By sblider in forum Symbian C++
    Replies: 2
    Last Post: 2004-08-03, 15:35
  5. how to read file
    By ddhungry in forum Symbian C++
    Replies: 1
    Last Post: 2002-12-13, 06:09

Posting Permissions

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