×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Regular Contributor
    Join Date
    Nov 2010
    Posts
    118

    Fileconnection NEED HELP

    hi, i am making an .xml file inside my fileconn.dir.photos.. i can successfully create the file... i want to read the contents of the xml as string... how do i do this? please help me

    the CODE:

    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 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());
            }
        }
    
        private void read(String path,String name)
        {
            try
            {
                String url = path + name;
                FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ);
                InputStream in1 = fconn.openInputStream();
                in1.read();
                Form asa=new Form("READ");
                asa.append(in1.toString());
                in1.close();
                Display.getDisplay(this).setCurrent(asa);
            }
            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, "tae.xml");
                read(path,"tae.xml");
            }
            if (c == exitCommand) this.notifyDestroyed();
        }
    }
    please point out where i got a WRONG code and please suggest a correct code.

    thank you so much!!

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

    Re: Fileconnection NEED HELP

    Please look at the JavaDocs for the InputStream class, and see what it says for the read() method.

    Also, please read the documentation for toString()... you'll find that InputStream inherits this method from Object.

    It's important that you know what these two methods do, and why your existing code doesn't work. Then, you might find this article from the wiki helpful.

    Graham.

Similar Threads

  1. fileconnection
    By csckid in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2010-09-14, 08:26
  2. FileConnection not available
    By stein in forum Mobile Java General
    Replies: 11
    Last Post: 2007-09-05, 06:30
  3. FileConnection
    By earamsey in forum Mobile Java General
    Replies: 1
    Last Post: 2006-11-14, 11:55
  4. help regarding Fileconnection
    By mottaisami in forum Mobile Java General
    Replies: 0
    Last Post: 2005-10-22, 06:24
  5. Fileconnection
    By lpinguin in forum Mobile Java General
    Replies: 0
    Last Post: 2005-05-10, 15:31

Posting Permissions

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