Results 1 to 7 of 7
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default Deleting a File that is opened

    I am reading a text file that is being used by some other process. I want to delete it after using. How this is possible.

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Hope this helps.... Google is your friend. :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks but it only works if file is not being used. I have a Java thread that is writing to the text file. The second thread tries to delete the file. Is it possible?

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by ravian View Post
    Thanks but it only works if file is not being used. I have a Java thread that is writing to the text file. The second thread tries to delete the file. Is it possible?
    Hmm.... it's my understanding that it might not be able to delete a file that is being used - that's just plain OS/Software fundamentals. Ever try deleting your Word or Open Office document that's currently open? You get a lovely popup message saying "you must close this file before you can delete it"... or something to that effect. But maybe there is a way in Java... maybe someone knows. What we do know, is that even though you have two threads, at some point the thread using the file will eventually stop using the file, unless you've implemented an infinite loop into the program.... interesting question nevertheless.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  5. #5
    DwayneWayne is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    It is possible... I reckon that you save your loaded data from the file in some sort of data-structure, array etc.

    You need to load the file and then remember to use the .close() method, then you should be able to overwrite/delete the file.

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Try this

    Hello DwayneWayne

    You can try the File.deleteOnExit() method. It should wait until your program finishes running and then deletes the file. However, this will not work, if some other program is using the file.

    Good luck! :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    it2051229 is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default Yeah.. Can java do it?

    Yeah.. i was also wondering if Java has the ability to stop a currently running process and then delete the file.. is that possible? if it is, how do i do it?? any work around tricks you have?

Similar Threads

  1. Deleting All rows in the JTable
    By surot in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 11:44 AM
  2. Reading an already opened file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-25-2008, 07:50 PM
  3. deleting elements
    By nalinda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 02:42 AM
  4. Replies: 0
    Last Post: 12-04-2007, 12:11 PM
  5. How to know the ports opened by JVM
    By sandeepspatil in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 02:51 PM

Posting Permissions

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