954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

deleting files from arraylist

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();
            
        }
    }
mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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?

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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...

regent_royal
Newbie Poster
20 posts since May 2008
Reputation Points: 6
Solved Threads: 0
 

i am adding a list of files like

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

and so forth.

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

Are they List or List?

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

Files2Delete.get(i).delete();
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

scratch my last post, you are adding strings

What does the println show you before the delete?

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

it shows the files in the arraylist

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

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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));            
        }
    }
mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

in fileDelete()

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

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

Ok just try for this...

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

regent_royal
Newbie Poster
20 posts since May 2008
Reputation Points: 6
Solved Threads: 0
 

looks to be ok

are the files open and you have permissions to them?

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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

regent_royal
Newbie Poster
20 posts since May 2008
Reputation Points: 6
Solved Threads: 0
 

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

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You