×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    Wrting to a file in J2me(error can't find the file)

    Hi I'm having trouble writing to a test file on my phone, I get an error saying It can't find the text file.But when I look in the system I can see that the file exsists. There could be a bug in my code .


    Code:
    OutputConnection connection = (OutputConnection)Connector.open("file:///E:/data/myfile.txt", Connector.WRITE );

    I'm using a Nokia N95


    Also if any has a good tutorial on how to write to a text from J2me IT would be greatly appricated


    thanks
    Kevin

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.io.*;
    import javax.microedition.io.*;
    import java.io.*;
    import java.util.*;
    import javax.microedition.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.io.file.*;
    
    public class FileConnection extends MIDlet implements CommandListener,Runnable {
      private Command exit, start;
      private Display display;
      private Form form;
        private String fileName;
      public FileConnection () 
      {
        display = Display.getDisplay(this);
        exit = new Command("Exit", Command.EXIT, 1);
        start = new Command("Start", Command.EXIT, 1);
        form = new Form("Write To File");
        form.addCommand(exit);
        form.addCommand(start);
        form.setCommandListener(this);
      }
      public void startApp() throws MIDletStateChangeException 
      {
        display.setCurrent(form);
      }
      public void pauseApp() 
      {
      }
      public void destroyApp(boolean unconditional) 
      {
      }
      public void commandAction(Command command, Displayable displayable) 
      {
        if (command == exit) 
        {
          destroyApp(false);
          notifyDestroyed();
        }
        else if (command == start) 
        {
        	Thread th = new Thread(this);
    	    th.start();
        }
      }
    public void run() {
    	
    	
        try 
        { 
    
    //Error probably here_______________________________
                   
    			  form.append("test1");
    			  OutputConnection connection = (OutputConnection)Connector.open("file:///E:/data/myfile.txt", Connector.WRITE );
    			  form.append("test2");
    			  OutputStream out = connection.openOutputStream();
    			  out.write(23423);
    			  out.flush();
    			  form.append("test3");
    			  out.close();
    			  connection.close();
    			  Alert alert = new Alert("Completed", "Data Written", null, null);
    			  alert.setTimeout(Alert.FOREVER);
    			  alert.setType(AlertType.ERROR);
    			  display.setCurrent(alert);   
    			  
    			  
    			  
        }
        catch( ConnectionNotFoundException error )
         {
           Alert alert = new Alert(
                "Error", "Cannot access file.", null, null);
           alert.setTimeout(Alert.FOREVER);
           alert.setType(AlertType.ERROR);
           display.setCurrent(alert);      
          }
          catch( IOException error )
          {
           Alert alert = new Alert("Error", error.toString(), null, null);
           alert.setTimeout(Alert.FOREVER);
           alert.setType(AlertType.ERROR);
           display.setCurrent(alert);      
          }
      }
    
    	// TODO Auto-generated method stub
    	
    }
    Last edited by kevin216; 2009-03-22 at 22:05.

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

    Re: Wrting to a file in J2me(error can't find the file)

    Hello,
    I recommend to use localized names in the file paths, there's an example at the bottom
    http://library.forum.nokia.com/topic...79F5BE26E.html

    Here are some additional examples
    http://wiki.forum.nokia.com/index.ph.../Data_Articles
    :Ruben

Similar Threads

  1. Emulator: Can't find class file. Error
    By linlearn in forum Mobile Java Tools & SDKs
    Replies: 6
    Last Post: 2007-03-05, 12:20
  2. File server: Compilation error in carbide.c++
    By justteam in forum Symbian C++
    Replies: 6
    Last Post: 2007-01-03, 23:44
  3. Replies: 1
    Last Post: 2005-05-26, 14:22
  4. Replies: 0
    Last Post: 2002-06-10, 12:24
  5. How can I find the correct Lib file????
    By Nokia_Archive in forum Symbian C++
    Replies: 1
    Last Post: 2002-06-01, 20:53

Posting Permissions

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