Results 1 to 5 of 5
  1. #1
    3speed is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default Help deleting a file

    Hi,

    I am trying to delete a file using file.delete();

    but doesn't to work. File is always there and file.delete always returns false with no errors.

    How can I debug?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Several questions:
    What OS?
    Can you delete the file manually?
    Is the File object refering to the file that you want to delete?
    Does File.exsits() return true?

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Put your name of the file instead of String fileName = "file.txt"; and try to see what will happen
    Java Code:
    
    
    import java.io.File;
    
    public class Delete {
      public static void main(String[] args) {
        String fileName = "file.txt";
        // A File object to represent the filename
        File f = new File(fileName);
    
        // Make sure the file or directory exists and isn't write protected
        if (!f.exists())
          throw new IllegalArgumentException(
              "Delete: no such file or directory: " + fileName);
    
        if (!f.canWrite())
          throw new IllegalArgumentException("Delete: write protected: "
              + fileName);
    
        // If it is a directory, make sure it is empty
        if (f.isDirectory()) {
          String[] files = f.list();
          if (files.length > 0)
            throw new IllegalArgumentException(
                "Delete: directory not empty: " + fileName);
        }
    
        // Attempt to delete it
        boolean success = f.delete();
    
        if (!success)
          throw new IllegalArgumentException("Delete: deletion failed");
      }
    
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Just another cross poster.
    Java Programming - Help deleting a file

    db

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First check the file cam delete manually. What the file are you trying to delete? Is that a file you create or a system dependent file. You cannot delete system dependent files, or sharing files with another process.

Similar Threads

  1. Deleting from an object
    By vitaminz in forum New To Java
    Replies: 7
    Last Post: 08-10-2008, 04:56 AM
  2. Deleting Contractions
    By theonly in forum New To Java
    Replies: 3
    Last Post: 04-27-2008, 08:44 PM
  3. Deleting All rows in the JTable
    By surot in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 11:44 AM
  4. Deleting a File that is opened
    By ravian in forum Advanced Java
    Replies: 6
    Last Post: 01-30-2008, 03:05 PM
  5. deleting elements
    By nalinda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 02:42 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
  •