×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    39

    Error accessing file

    Hi All,
    I've been having some problem accessing a file.
    Device: Nokia E71
    Plot: I read some data from a file (if exists), process it, and replace the data with new one. if file does not exist, I create the file with new data.
    Problem: When file does not exist, Program works - new file is created - and is accessible. but when I read (and as you notice in the code, close it after reading is finished), file is not accessible for writing. An exception is thrown saying "File is already in the use" when the file should be trunkated. What is your suggestion?
    Code:
    //...
    import javax.microedition.io.*;
    import javax.microedition.io.file.*;
    import java.io.*;
    import java.util.*;
    
    public class myClass extends MIDlet implements CommandListener 
    {
    	public void ReadFile(String fileUrl)
        {
            String temp = System.getProperty("microedition.io.file.FileConnection.version");
            if ((temp!="")&&(temp!=null))
            {
                FileConnection fc = null;
                try
                {
                    fc = (FileConnection)Connector.open(fileUrl, Connector.READ_WRITE);
                    if(fc.exists()&&(fc != null))
                    {
                        DataInputStream dis = fc.openDataInputStream();
                        String inStr;
                        String contents = "";
                        try
                        {
                            while ((inStr = dis.readUTF()) != null )
                            {
                                contents = contents + inStr;
                            }
                        }
                        catch(EOFException eofex)
                        {
                            /*Alert a = new Alert("end of file reached", eofex.getMessage(), null, AlertType.ERROR);
                            a.setTimeout(10000);*/
                            switchDisplayable(null, getForm());
                        }
                        catch(Exception e)
                        {
                            Alert a = new Alert("file read exception", e.getMessage(), null, AlertType.ERROR);
                            a.setTimeout(5000);
                            switchDisplayable(a, getForm());
                        }
                        DoSomething();
                    }
                    else
                    {
                        DoSomethingElse();
                        fc.create();
                    }
                }
                catch(Exception e)
                {
                    Alert a = new Alert("Error creating file", e.getMessage(), null, AlertType.ERROR);
                    a.setTimeout(10000);
                    switchDisplayable(a, getForm());
                }
                finally
                {
                    try{
                        fc.close();
                    }
                    catch(Exception exr)
                    {
                        Alert a = new Alert("error closing read connection", exr.getMessage(), null, AlertType.ERROR);
                        a.setTimeout(10000);
                        switchDisplayable(a, getForm());
                    }
                }
            }
        }
    	public void WriteFile(String fileUrl,String myText)
        {
            String temp = System.getProperty("microedition.io.file.FileConnection.version");
            if ((!temp.equals(""))&&(temp!=null))
            {
                boolean DebugResume = true;
                DataOutputStream dos = null;
                try
                {
                    FileConnection fc = (FileConnection)Connector.open(fileUrl, Connector.READ_WRITE);
                    try{
                        DebugResume = false;
                        if (fc.exists())
    					{
    						try
    						{
    							fc.truncate(0);//exception is thrown here!! <----
    						}
    						catch(IOException ioe)
    						{
    							Alert a = new Alert("truncating-access denied", ioe.getMessage(), null, AlertType.ERROR);
    							a.setTimeout(10000);
    							switchDisplayable(a, getForm());
    						}
    						catch(ConnectionClosedException cce)
    						{
    							Alert a = new Alert("truncating-connection closed", cce.getMessage(), null, AlertType.ERROR);
    							a.setTimeout(10000);
    							switchDisplayable(a, getForm());
    						}
    						catch(SecurityException se)
    						{
    							Alert a = new Alert("truncating-security issue", se.getMessage(), null, AlertType.ERROR);
    							a.setTimeout(10000);
    							switchDisplayable(a, getForm());
    						}
    						catch(IllegalModeException ile)
    						{
    							Alert a = new Alert("truncating-illegal mode exception", ile.getMessage(), null, AlertType.ERROR);
    							a.setTimeout(10000);
    							switchDisplayable(a, getForm());
    						}
    						catch(IllegalArgumentException iae)
    						{
    							Alert a = new Alert("truncating-illegal argument", iae.getMessage(), null, AlertType.ERROR);
    							a.setTimeout(10000);
    							switchDisplayable(a, getForm());
    						}
    						catch(Exception e)
    						{
    							Alert a = new Alert("truncating existing file", e.getMessage(), null, AlertType.ERROR);
    							a.setTimeout(10000);
    							switchDisplayable(a, getForm());
    						}
    					}
    					else
    					{
    						try
    						{
    							fc.create();
    						}
    						catch(Exception e)
    		                {
    		                    Alert a = new Alert("Creating file to write", e.getMessage(), null, AlertType.ERROR);
    		                    a.setTimeout(10000);
    		                    switchDisplayable(a, getForm());
    		                }
    					}
    				}
    				catch(Exception ex1)
    				{
    					Alert a = new Alert("Error opening fileConection", ex1.getMessage(), null, AlertType.ERROR);
    					a.setTimeout(50000);
    					switchDisplayable(a, getForm());
    				}
    				try{
                        dos = new DataOutputStream(fc.openDataOutputStream());
                        dos.writeUTF(myText);
                        if (dos != null)
                        {
                            dos.close();
                        }
                    }
                    catch(Exception ex2)
                    {
                        Alert a = new Alert("Error writing to file", ex2.getMessage(), null, AlertType.ERROR);
                        a.setTimeout(10000);
                        switchDisplayable(a, getForm());
                    }
    				try{
                    if (fc.isOpen()) fc.close();
                    }
                    catch(Exception ex3)
                    {
                        Alert a = new Alert("Error Closing write connection", ex3.getMessage(), null, AlertType.ERROR);
                        a.setTimeout(50000);
                        switchDisplayable(a, getForm());
                    }
    			}
    			catch(Exception e)
                {
                    Alert a = new Alert("file exception4", e.getMessage(), null, AlertType.ERROR);
                    a.setTimeout(50000);
                    switchDisplayable(a, getForm());
                }			
            }
                
        }
    	private void Work() 
    	{  
            try
            {
                DirAddress = System.getProperty("fileconn.dir.recordings");//where to read/write file
    			FileAddress = "myfile.txt";
                if(DirAddress!=null)
                {
                    FileAddress = System.getProperty("fileconn.dir.recordings")+ FileAddress;
                }
            }
            catch(Exception e)
            {
    			String directory = "X",
                FileAddress = "file:///" + getRoots() + Directory+"/"+ FileAddress;
                DirAddress = "file:///" + getRoots() + Directory;
            }
            ReadFile(FileAddress);
            DoSth();
            WriteFile(FileAddress,myText);
    		DoSthElse();
        }
    }
    Last edited by kavehmb2000; 2009-04-04 at 12:25.

  2. #2
    Super Contributor
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330

    Thumbs up Re: Error accessing file

    In Finally block close all the connection as well as close all the streams u opened too. After closing connection and streams set them to null.
    thanks,
    jitu_goldie..

    KEEP TRYING..

Similar Threads

  1. Regarding BMCONV
    By sanah007 in forum Symbian
    Replies: 2
    Last Post: 2009-05-26, 12:47
  2. Error : Cannot find file : for DUMMIES
    By PACALA_BA in forum Symbian
    Replies: 7
    Last Post: 2008-07-06, 13:18
  3. Problem with pyobfuscate
    By JOM in forum Symbian
    Replies: 3
    Last Post: 2008-06-20, 22:47
  4. Problem opening file from a dynamic path
    By didienGanteng in forum Symbian
    Replies: 12
    Last Post: 2007-06-27, 11:13
  5. Contacts fields order??
    By timatima in forum Symbian
    Replies: 1
    Last Post: 2007-06-08, 13:51

Posting Permissions

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