×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor
    Join Date
    Feb 2009
    Posts
    52

    Question About Write Text File In J2me

    Hi,

    Can any one tell me that how to write text file in j2me? I got this code from net for write text file in j2me but it is not working. What is problem in this code?

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

    public class WriteMIDlet extends MIDlet implements CommandListener {
    private TextBox textbox;
    private String photos = "fileconn.dir.photos";
    private Command saveCommand;
    private Command exitCommand;
    private String path;

    public void startApp() {
    textbox = new TextBox("WriteMIDlet", "", 1000, TextField.ANY);
    saveCommand = new Command("Save", Command.SCREEN, 1);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    textbox.addCommand(saveCommand);
    textbox.addCommand(exitCommand);
    textbox.setCommandListener(this);
    Display.getDisplay(this).setCurrent(textbox);
    path = System.getProperty(photos);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    private void saveFile(String path, String name) {
    try {
    String url = path + name;
    String string = textbox.getString();
    byte data[] = string.getBytes();
    FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
    if (!fconn.exists()) {
    fconn.create();
    }
    OutputStream ops = fconn.openOutputStream();
    ops.write(data);
    ops.close();
    fconn.close();
    }
    catch (IOException ioe) {
    System.out.println("IOException: "+ioe.getMessage());
    }
    catch (SecurityException se) {
    System.out.println("Security exception:" + se.getMessage());
    }
    }

    public void commandAction(Command c, Displayable d) {
    if (c == saveCommand) saveFile(path, "readme.txt");
    if (c == exitCommand) this.notifyDestroyed();
    }
    }

  2. #2
    Nokia Developer Expert
    Join Date
    May 2007
    Location
    Mexico D.F
    Posts
    351

    Re: About Write Text File In J2me

    Hello,
    Are you using a phone or an emulator, if so which phone?


    More examples here
    http://wiki.forum.nokia.com/index.ph.../Data_Articles
    :Ruben

  3. #3
    Regular Contributor
    Join Date
    Feb 2009
    Posts
    52

    Re: About Write Text File In J2me

    Hi rdrincon,

    Thanks for ur reply..
    I am using emulator nokia S60. I got code on ur given link but it is not working. I have pasted this code in my initail thread plz check it and tell me what is problem?

    Thanks
    abhi4u

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    41

    Re: About Write Text File In J2me

    Hi,

    I've try that script using Eclipse Pulsar on Windows XP (in emulator of J2ME).

    It's run but when I write a midlet and push the "save" button, why it can't?

    What must I do?


    Thanks.

    FYI :
    I just read the info in http://wiki.forum.nokia.com/index.ph...ile_in_Java_ME
    "Then the text can be saved to readme.txt file, which is in this case created under Gallery's Images folder."

    My question :
    where is the path (Gallery's Images folder)? Or I must create that folder? If yes, where?


    =============
    I've try it again and found the message error like this :

    Project "WriteMIDlet" loaded
    Project settings saved
    Building "WriteMIDlet"
    Build complete
    Running with storage root DefaultColorPhone
    java.lang.IllegalArgumentException: no ':' in URL
    at javax.microedition.io.Connector.openPrim(+37)
    at javax.microedition.io.Connector.open(+15)
    at javax.microedition.io.Connector.open(+6)
    at WriteMIDlet.saveFile(+40)
    at WriteMIDlet.commandAction(+18)
    at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
    at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
    at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
    at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)

    what should I do?
    Last edited by bytescode; 2009-07-26 at 17:28. Reason: error message but ddon't know what must i do

  5. #5
    Nokia Developer Expert
    Join Date
    May 2007
    Location
    Mexico D.F
    Posts
    351

    Re: About Write Text File In J2me

    Seems the error is in the URL, please print it and let us know what it says.
    :Ruben

  6. #6
    Nokia Developer Champion
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330

    Thumbs up Re: About Write Text File In J2me

    Please mention the URI used by urself.
    thanks,
    jitu_goldie..

    KEEP TRYING..

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: About Write Text File In J2me

    Hi,

    The problem is that we cannot see the "path" that composes your target URL. So you should provide one accordingly.

    Br,
    Juarez Alvares Barbosa Junior - Brazil

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Re: About Write Text File In J2me

    Hello

    So did you find an answer yet?
    If you havent please reply cause I have resolved it...

    Thanx

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

    Re: About Write Text File In J2me

    Did you try this example?

    Please tell us what code you're using, and what problem you're having.

    Graham.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    21

    Re: About Write Text File In J2me

    Quote Originally Posted by grahamhughes View Post
    Did you try this example?

    Please tell us what code you're using, and what problem you're having.

    Graham.
    hi i need entire source for reading text file using j2me in emulator

  11. #11
    Registered User
    Join Date
    Jul 2012
    Location
    South America
    Posts
    43

    Re: About Write Text File In J2me

    im using that code too .. but i dont think so if its working ..

    but i'll try it

    then you can use that if you had your
    application in need ..

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    31

    Re: About Write Text File In J2me

    Always it is good to write the connections related code written in a separate thread , be it is related fileconnection or networkconnection and so on.
    Taking the same code mentioned in the link http://www.developer.nokia.com/Commu...ile_in_Java_ME . I have moved the entire logic of savefile method to a thread and it worked for me fine. So try doing the same you could achieve the result.
    Last edited by venky2810; 2012-09-26 at 05:59.
    Regards,
    Venkatesh

Similar Threads

  1. very simple middlet class to write to write a text file
    By a_sina_a in forum Mobile Java General
    Replies: 10
    Last Post: 2008-03-07, 16:35
  2. How to read and write a binary file in J2ME
    By kamalakshan in forum Mobile Java General
    Replies: 6
    Last Post: 2008-02-29, 11:28
  3. Replies: 2
    Last Post: 2007-12-13, 12:19
  4. how to write in a text file using J2me
    By b7b7 in forum Mobile Java Tools & SDKs
    Replies: 2
    Last Post: 2007-07-01, 11:36
  5. Write file in 3rd generation with j2me
    By loay980 in forum Mobile Java Networking & Messaging & Security
    Replies: 13
    Last Post: 2006-03-30, 08:03

Posting Permissions

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