How do i delete Older files from the directory before create a new file?

Hi,
How do i delete older files in a particular directory,
the senorio is count the number of .txt files in a directory and delete the older files if file count is more than 10. (if i add 11th file the very first file has to be deleted)
i have written the code to count the files and delete , but it is deleting all the files instead of older file
public class ExtensionFilter implements FilenameFilter {
  private String extension;
  public ExtensionFilter( String extension ) {
    this.extension = extension;            
  public boolean accept(File dir, String name) {
    return (name.endsWith(extension));
public class FileUtils{
  public static void main(String args[]) throws Exception {
    FileUtils.deleteFiles("c:/countfile/", ".txt");
  public static void deleteFiles( String directory, String extension ) {
    ExtensionFilter filter = new ExtensionFilter(extension);
    File dir = new File(directory);
    String[] list = dir.list(filter);
    File file;
    if (list.length == 0) return;
    for (int i = 0; i < list.length; i++) {
      //file = new File(directory + list);
file = new File(directory, list[i]);
if ((list[i]).length()>=10)
     System.out.print(file + " deleted : " + file.delete());
Thanks,
Jamin Rosina                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

What your code is doing now, is deleting all filenames that are longer than 10 characters.
Your problem lies in the line:
if ((list).length()>=10)

Similar Messages

  • I have an alias of one of my external hard drives in my trash. When I click on "show original" all my external H D 's and my Mac hard drive show up. How do I delete this icon from the trash without loosing all my files?

    I have an alias of one of my external hard drives in my trash. When I tried to delete it, it showed that I was deleting 80,000 files and counting. I stopped the delete process and when I clicked on "show original" all my external H.D.'s and my Mac hard drive show up. How do I delete this alias from the trash without loosing all my files? When I tried to drag it back to the desk top, it just made a duplicate and remained in the trash. When I turned off the external H.D., the trash was then empty. Tried rebooting the computer and then turning the external hard drive back on and the alias shows up in the trash again. Much help appreciated.
    Rich

    In Finder's Menu, select Go menu>Go to Folder, and go to "/volumes". (no quotes)
    Volumes is where an alias to your hard drive ("/" at boot) is placed at startup, and where all the "mount points" for auxiliary drives are created for you to access them. This folder is normally hidden from view.
    Drives with an extra 1 on the end have a side-effect of mounting a drive with the same name as the system already think exists. Try trashing the duplicates with a 1 or 2 if there are no real files in them, and reboot.
    If it does contain data...
    http://support.apple.com/kb/TS2474

  • My apple account was hacked so cannot get my password. how do i delete my account from my phone to create a new iCloud account. It asks for a password before I can delete my account?

    i am unable to delete my Icloud account off my phone as it has been hacked and I am unable to retrieve a new password. Is there anyway I can remove it?

    You won't be able to remove it without the password.  If you can't reset it yourself, contact the Apple account security team for your country for assistance: Apple ID: Contacting Apple for help with Apple ID account security.  If your country isn't listed, try contacting iTunes store support: https://ssl.apple.com/emea/support/itunes/contact.html.

  • Ive had a lap top from a friend, she cleared everything off it but ive downloaded itunes and all her music has appeared in the itunes library. How can i delete her music from the itunes library?

    ive got a laptop from a friend, she cleared everything off it. ive downloaded itunes and for some reason all her music has appeared in the library. How do i delete her music from the itunes libary so i can add my own music?

    In iTunes, select the music you want to delete (⌘+a to select all) and hit Delete on your keyboard.
    Also delete the music files in Finder. Also make sure you have your own Apple ID and iTunes account and logged in.

  • How do I delete a movie from the iTunes-cloud?

    How do I delete a movie from the iTunes-cloud? I don't want to hide it I want to delete it even if it means I can't download it again..

    Oh sure you can delete it from the computer. I thought that you wanted to permanently delete it so that you can never access it again. In fact, in some countries, you can delete movies and then download that again at no charge as long as you use the same Apple ID.
    Launch iTunes on your computer and click on the movies tab under the Library heading, find the movie and right click on it and select Delete.

  • How can I delete an app from the cloud??

    how can I delete an app from the cloud, so nobody can download it again to my phone?? please I tried from the computer but it just hide the app from purchased, and still appears in the app store with the cloud on it

    At the present time, you can't hide or delete an app from your purchased list on your phone.  Prior to iOS 6 you could hide it; there has never been an option to delete it.

  • How can i delete purchased items from the cloud in iTunes

    how can i delete purchased items from the cloud in iTunes

    I had this problem as well. You can't delete them but you can hide them by swiping across from the cloud to. The other side then pressing hide

  • Using a iPad.  How do you delete unwanted address from the drop down list.  I typed in a wrong address once, and now it keeps showing up.

    Using a iPad.  How do you delete unwanted address from the mail drop down list.  I typed in a wrong address once, and now it keeps showing up.

    Winfax1-
    Look in your Contacts App and see if the unwanted address is entered there.  If it is, you can delete it there.
    Fred

  • How do I delete duplicate icons from the dock of an Imac 8.1?

    How do I delete duplicate icons from the dock of an Imac 8.1?

    No dragging to trash doesn't work. Just keep dragging them off. If they are stacked, which shouldn't happen, then when you delete one the other is still there.
    Just how did you get duplicates? Did you put them there? If not then you have other problems and I'm not sure how to correct it.

  • There is an album on a smart playlist on my ipod that is not on the music section of my ipod. I am not synced to any computer. How do I delete this album from the playlist and presumably my computer?

    There is an album on a smart playlist on my ipod that is not on the music section of my ipod. I am not synced to any computer. How do I delete this album from the playlist and presumably my computer?
    Thanks!

    You will need to restore your phone as a new one using the itunes on your computer.  This WILL erase everything on the phone but it will let you change the name of the phone and then you can add content to it.

  • HT201272 How can we delete apps, etc from the Previously Purchased list so that they don't show there anymore?

    How can we delete apps, etc from the Previously Purchased list so that they don't show there anymore?

    You can't currently permanently delete purchases, all you can do is hide them so that they are less visible : hiding and unhiding purchases

  • Hi! how can I delete a book from the iCloud? I bought the wrong book ... is for adults and I have kids...so

    Hi! how can I delete a book from the iCloud? I bought the wrong book ... is for adults and I have kids...so

    The most comprehensive delete method I know about is to delete the book from iTunes on the computer you sync your iPad with.  You'll be asked if you're sure, and it will be deleted from your iPad and any other i-Device you sync to.
    I think that's the best you can do.

  • How can I delete tv shows from the iCloud on my Apple TV?

    I recently purchased Apple TV (3rd gen) and need to delete a tv show that appears there.  There's no trash can icon on the screen as some posts state--just an iCloud icon.  How do I delete an item from the iCloud so it will no longer appear on the Apple TV?

    You can't hide it AFAIK on AppleTV - do it via iTunes.
    In iTunes elect the store home page, and choose Purchases in teh QuickLinks on the right.
    Choose TV Shows and hover over the one to hide and there should be a small x to hide it.
    If you need it back go into your actual account settings in iTunes and choose to unhide there.
    AC

  • How can we delete a line from the table control .

    hi all
    how can we delete a line from the table control .
    situation is.
    created table control in se51 which will display the data of a table.
    how could i select a line from the table control ?
    how could i delete the selected lines form the table.
    thanks in advance

    Change the Table Control attributes such that user can only select a single record(row).
    <u>Tip to delete a selected record</u>
    1) write a module 'Mark' in the PAI as below
    PROCESS AFTER INPUT.
    MODULE cancel AT EXIT-COMMAND.
    LOOP WITH CONTROL table_view.
    MODULE read_table_control.
    FIELD flag MODULE mark ON INPUT.
    ENDLOOP.
    MODULE user_command_0100.
    2) Module Mark is below.
    MODULE mark INPUT.
    CHECK flag = 'X'.
    x = table_view-top_line + sy-stepl - 1.
    Delete itab INDEX x.
    ENDMODULE. " mark INPUT
    Table_view is the TableControl Name.
    'flag' is of type char(1) available in the Internal table which was assigned to the select option in the table control.
    <u>award if uesful</u>
    Regards,
    Sudheer

  • How do i delete messages (saved) from the usage settings. I have no messages in my inbox but it says i am taking up 2.3GB

    How do i delete messages (saved) from the usage settings?
    I have no messages in my inbox but it says i am taking up 2.3GB
    Its an IPhone 5 16GB
    IOS 7.1.2

    i realised i have this 200mb of messages this morning & tried deleting my messages but it started to hang half way and my phone gets very hot. thn i do pressed my lock & home button to reset the phone, thn it cool atfer awhile but i got all my messges back lol..
    thn i found out, after deleting all the unwanted messages, sync to itunes, thn all the messages is gone for good. hope it helps?

Maybe you are looking for