Results 1 to 11 of 11
  1. #1
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Exclamation How to delete a .txt file if the contents is empty

    I'm trying to create a method which checks if a certain text file is empty, and if so it deletes it. However, the program seems to freeze for one reason or another when executing this part.

    Java Code:
    try {
                BufferedReader in = new BufferedReader(new FileReader("File List.txt"));
                String str;
                while ((str = in.readLine()) == null) {
    
                    boolean success = (new File("File List.txt")).delete();
                }
                in.close();
            } catch (IOException e) {
            }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See your cross-post.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Rule number 1: never ignore exceptions (except in very extra-ordinary circumstances). Your catch block is empty, and so the first thing you should do is fill it with at least a print stack trace statement.

    Next, it appears as if you're trying to delete a file while you still have it open, is that true, or am I reading the code incorrectly?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Notify of what?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by SeanC View Post
    Notify of what?
    Of the cross-post linked to above and any other cross-posts. You should do this if you ever cross-post for several reasons:
    1) Our time is valuable to us and we don't want to duplicate work already done elsewhere in a cross-post. Your posting links will allow us to check the cross-posts before answering.
    2) Your ultimate solution may be posted in a cross-post, and so if in the future others with the same problem search this forum and happen upon your thread, if you don't post the link, they won't see the solution.

    Thanks for your cooperation.

  7. #7
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Of the cross-post linked to above and any other cross-posts. You should do this if you ever cross-post for several reasons:
    1) Our time is valuable to us and we don't want to duplicate work already done elsewhere in a cross-post. Your posting links will allow us to check the cross-posts before answering.
    2) Your ultimate solution may be posted in a cross-post, and so if in the future others with the same problem search this forum and happen upon your thread, if you don't post the link, they won't see the solution.

    Thanks for your cooperation.
    Ah of course :) Ok, thanks will do!

  8. #8
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    It is simple to do it.

    Define one string variable.
    Try to read line of the file and put it's value in that variable.
    If value is null that means that file is empty
    Last edited by cselic; 08-15-2010 at 10:34 PM. Reason: typos

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    No. Simply create the File object and call length and call delete if that is 0 (or less). No need to read anything.

  10. #10
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    No. Simply create the File object and call length and call delete if that is 0 (or less). No need to read anything.
    Thanks, it worked! :) Greatly appreciate everyone's help!

  11. #11
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Yes. It works.

    Java Code:
    import java.io.File;
    
    public class EmptyFile {
    	
    	public static void main(String[] args) {
    		File filename = new File("empty.txt");
    			
    			if(filename.length() == 0) {
    				filename.delete();
    				System.out.println("File has been deleted");
    			}
    	}
    }
    SeanC mark this thread as solved.

Similar Threads

  1. Delete A File
    By nitinverma in forum CLDC and MIDP
    Replies: 13
    Last Post: 06-15-2010, 08:12 AM
  2. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  3. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  4. Delete Empty Spaces...
    By ohytheng in forum New To Java
    Replies: 1
    Last Post: 04-15-2009, 09:59 PM
  5. Replies: 9
    Last Post: 07-10-2008, 04:52 PM

Tags for this Thread

Posting Permissions

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