Why wont this delete the files in the arraylist. i checked the arraylist and the files are there. i even added a "\\" to the files and still nothing. i dont get any errors.

private void fileDelte ()
    {   
        for (int i =0; i<Files2Delete.size(); i++)
        {
            File f = new File(Files2Delete.get(i));
            f.delete();
            
        }
    }

Recommended Answers

All 26 Replies

A few well-placed System.out.println() statements to verify what you are trying to delete would be a good place to start.

Definitely agree with ezzaral. Get your path, then println it, then call delete.

Also there are numerous things that could go wrong with file access/permissions. Make sure you either throw the exception or catch it and println it as well

yes i already tried that, but my question is would the code work. i usually program with c# not java. so is my code above fine. basically does f.delete() is that the correct funtion to delete files in java?

short answer: yes (if you have permissions)

long answer: sort of, you can delete directories with this as well, but if its a directory thats not empty, it will not delete, ensure your files are not directories

Yes, which is clearly documented here in the API. Those docs are your best friend if you plan on doing anything at all in Java.

Are you adding directly file to arraylist or it's path???? If you have already added path as string in arraylist and then get this string via new File object then this one is right...

they are files no directories. just a list of files.

i am adding a list of files like

c:\testing.txt
c:\whatever\testing2.txt
c:\something\else\testing3.txt

and so forth.

Are they List<File> or List<String>?

From what you described it sounds like file if so you can use this

Files2Delete.get(i).delete();

scratch my last post, you are adding strings

What does the println show you before the delete?

it shows the files in the arraylist

c:\testing.txt
c:\whatever\testing2.txt
c:\something\else\testing3.txt

complete code

private ArrayList<String> Files2Delete = new ArrayList<String>();
    private String fName = "c:\\TestFile.txt";
   
    public Files()
    {
        this.fileCollection();
        this.fileDelte();
    }
    
    private void fileCollection () 
    {
        try         
        {
            File file = new File(fName);
            Scanner in = new Scanner(file);

            while (in.hasNextLine())
            {
                String line = in.nextLine();
                Files2Delete.add(line);
            }
            in.close();
        }
        catch(Exception e)
        {
            String s = e.toString();
        }
    }
    
    private void fileDelte ()
    {   
        for (int i =0; i<Files2Delete.size(); i++)
        {
            File f = new File(Files2Delete.get(i));            
        }
    }

in fileDelete()

inside the for loop after the File f statement i also have:
f.delte();

Ok just try for this...

c:\\testing.txt
c:\\whatever\\testing2.txt
c:\\something\\else\\testing3.txt

looks to be ok

are the files open and you have permissions to them?

yes i have permission. it shouldnt matter,but i create blank files just for testing to make sure it actually deletes.

i am using XP pro SP3

ahhhh, when you create the files did you call the close() method?

Check this code...

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class ArrayListFileExample {
    public static void main(String[] args) throws IOException {
        ArrayList objArrayList = new ArrayList();
        String strFileName = "c://tempFileExample1.txt";
        objArrayList.add(strFileName);
        File f = new File(strFileName);
        f.createNewFile();

        strFileName = "c://tempFileExample2.txt";
        objArrayList.add(strFileName);
        f = new File(strFileName);
        f.createNewFile();

        strFileName = "c://tempFileExample3.txt";
        objArrayList.add(strFileName);
        f = new File(strFileName);
        f.createNewFile();

        // comment this part to check file is created or not.... after checking uncomment this file and check once again c: drive...
        for(int i=0;i<objArrayList.size();i++){
            new File((String) objArrayList.get(i)).delete();
        }
    }
}

yes ur code works, but i already have a predefined list of files. i am not creating the files. the files are already there.

try changing your code to this, at least while debugging

for (int i =0; i<Files2Delete.size(); i++)
 {
     String path = Files2Delete.get(i);
      File f = new File(path);  
      System.out.println("File: " + path + "canonical path: " + f.getAbsolutePath() + 
        			" can write:" + f.canWrite() + " can read" + f.canRead());
      f.delete();
}

Code has already been provided and your unrelated question in another forum has absolutely no bearing on this poster's current issues.

commented: The posting enforcer!!! +2

Ya, I know it's totly unrelated but link is another thread created by me... I am searching solution for that one.. If any one know answer then.... give solution....

commented: Do not use this person's thread to ask for help on your unrelated post. -2

on the above code i got the following output:


File: pathcanonical path: c:\testing.txt can write:false can readfalse

Sorry, somehow my exists didn't make it in

try with this

System.out.println("File: " + path + " canonical path: " + f.getAbsolutePath() + " exists:" + f.exists() " can write:" + f.canWrite() + " can read" + f.canRead());

Well, are you sure the files exist in that location? And what does file.exists() return for those files? If those are correct paths to files that do exist then the only other reason the delete would fail would be that the files are locked exclusively by another process. You are the only one who can determine that. Can you delete one of them through Explorer?

Edit: Bleh, cross-posted. This is a bit redundant with dickersonka's post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.