0

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.

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) {
        }
3
Contributors
6
Replies
9
Views
6 Years
Discussion Span
Last Post by SeanC
0

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.

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) {
        }

Hello i dont think you need the BufferedReader in = new BufferedReader at all!!!
check out this this code....

import java.io.File;

public class Delete {
  public static void main(String[] args) {
    String fileName = "file.txt";
    // A File object to represent the filename
    File f = new File(fileName);

    // Make sure the file or directory exists and isn't write protected
    if (!f.exists())
      throw new IllegalArgumentException(
          "Delete: no such file or directory: " + fileName);

    if (!f.canWrite())
      throw new IllegalArgumentException("Delete: write protected: "
          + fileName);

    // If it is a directory, make sure it is empty
    if (f.isDirectory()) {
      String[] files = f.list();
      if (files.length > 0)
        throw new IllegalArgumentException(
            "Delete: directory not empty: " + fileName);
    }

    // Attempt to delete it
    boolean success = f.delete();

    if (!success)
      throw new IllegalArgumentException("Delete: deletion failed");
  }

}

you can use f.length to check size of the file e.g

if(f.length()<1)
{
boolean success=f.delete();

}

Hope you get..

-1

That's already been siad, and if you check out the cross-post, it's already been solved, the OP simply hasn't been considerate enough to mark it as such, and, now, probably never will.

0

That's already been siad, and if you check out the cross-post, it's already been solved, the OP simply hasn't been considerate enough to mark it as such, and, now, probably never will.

What are you talking about? I marked it as solved before the reply was posted...

0

Then I apologise, but I, personally, did not notice that until now.

No problem, as far as I know the only way to tell that a post is solved is by reading the "Marked Solved" at the top which is quite small - so I guess it could be missed.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.