Results 1 to 14 of 14

Thread: Delete A File

  1. #1
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default Delete A File

    I want To delete a File.
    But when I delete a File Like this

    if (fconn.exists())
    {
    fconn.delete();
    }
    But It gives a Error Like This.

    IOException: cannot delete file.

    What Should I Do.

  2. #2
    HerrK. is offline Member
    Join Date
    Jun 2010
    Location
    Berlin
    Posts
    22
    Rep Power
    0

    Default

    Well, I don't think that you could get some IOException by calling fconn.delete() while this method just don't throws any IOException but returns a boolean whether the file could be deleted or not.

    While the File.exists() method don't raise an IOException, either, your problem might be somewhere else. It would be interesting to get the whole context and the full exception.

  3. #3
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    Ok Herrk,
    I will check my Application.Ok

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    But It gives a Error Like This.
    Can you copy and paste the full text of the error messages here?

    Is the file flagged R/O?

  5. #5
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    The Error Message is Only like this.

    IOException: cannot delete file.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Is that the full text of the error message?
    That looks like an edited version.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Post the full code for the method.

  8. #8
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    public void updatefile()
    {
    String totfile=new String(sb1.toString());
    System.out.println(totfile);
    byte data[] = totfile.getBytes();
    try
    {
    FileConnection fconn = (FileConnection)Connector.open("file:///root1/rsstitles.txt",Connector.READ_WRITE);
    if(!fileExists)
    {
    fconn.delete();
    }
    OutputStream ops = fconn.openOutputStream();
    ops.write(data);
    ops.close();
    fconn.close();
    }
    catch (IOException io)
    {
    System.out.println("IOException: "+io.getMessage());
    }
    catch (SecurityException se)
    {
    System.out.println("Security exception:" + se.getMessage());
    }
    }

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Instead of
    Java Code:
    System.out.println("IOException: "+io.getMessage());
    put

    Java Code:
    io.printStackTrace();
    instead and post the full message that you get.

  10. #10
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    This is the Complete Error:


    java.io.IOException: cannot delete file
    at com.sun.cdc.io.j2me.file.DefaultFileHandler.delete (), bci=0
    at com.sun.cdc.io.j2me.file.Protocol.delete(), bci=71
    at RssNews.WirelessNews.updatefile(WirelessNews.java: 420)
    at RssNews.WirelessNews.commandAction(WirelessNews.ja va:196)
    at javax.microedition.lcdui.Display$ChameleonTunnel.c allScreenListener(), bci=46
    at com.sun.midp.chameleon.layers.SoftButtonLayer.proc essCommand(), bci=74
    at com.sun.midp.chameleon.layers.SoftButtonLayer.comm andSelected(), bci=11
    at com.sun.midp.chameleon.layers.MenuLayer.keyInput() , bci=290
    at com.sun.midp.chameleon.CWindow.keyInput(), bci=38
    at javax.microedition.lcdui.Display$DisplayEventConsu merImpl.handleKeyEvent(), bci=17
    at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
    at com.sun.midp.events.EventQueue.run(), bci=179
    at java.lang.Thread.run(Thread.java:619)

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The API specs (which you should have with you when writing code) state that the delete method throws an IOException
    If the target is a directory and it is not empty, the connection target does not exist or is unaccessible, or an unspecified error occurs preventing deletion of the target.
    So you need to check for all of that.

  12. #12
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    Sorry Guy,
    I am not Understanding what r u saying.
    I have tried all of the things.
    And at the end I am change my Logics of the code.

    I have sent u that Method.
    Can u change my code send me.
    Plz don't mind. If u have time plz Do for me.

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Which part of my reply do you not understand?
    Just check the items mentioned in the API specs.
    It's obviously not a directory so the first part is clear.
    You can check if the file exists by using the FileConnection.exists method. If it does exist then the reason for the exception is that the file is
    unaccessible, or an unspecified error occurs preventing deletion of the target.

  14. #14
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

Similar Threads

  1. Unbale to delete and rename file
    By ddatta8 in forum New To Java
    Replies: 0
    Last Post: 01-31-2009, 01:16 PM
  2. How to Modify,Delete data in File Txt???
    By hungleon88 in forum Advanced Java
    Replies: 9
    Last Post: 09-24-2008, 03:19 AM
  3. Delete From .txt file
    By Sarinam in forum New To Java
    Replies: 86
    Last Post: 06-28-2008, 10:17 AM
  4. problem when I try to delete a file
    By tommy in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 02:44 PM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 08:11 AM

Posting Permissions

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