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

Dani AI

Generated

Two things are tripping you up. First, readLine() returns null only at EOF. Using while ((str = in.readLine()) == null) makes an endless loop on an empty file because the condition is always true and never advances state. Second, you are trying to delete the file while it is still open. On Windows the delete will fail while a stream holds the file handle, so you keep looping and it looks like a freeze. As and point out below, you do not need to read the file at all just to see if it is empty.

A simple, safe approach is to check the length and delete without opening a reader. Prefer NIO so failures throw instead of silently returning false:

import java.nio.file.*;
import java.io.IOException;

Path p = Paths.get("File List.txt");

try {
    if (Files.exists(p) && Files.size(p) == 0L) {
        Files.delete(p); // throws if deletion fails
    }
} catch (IOException e) {
    // log or handle as needed
}

If your definition of empty means "only whitespace or newlines," peek for the first non-whitespace character, then delete. Do this after closing any previous streams:

import java.nio.file.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

static boolean isWhitespaceOnly(Path p) throws IOException {
    try (BufferedReader r = Files.newBufferedReader(p, StandardCharsets.UTF_8)) {
        int ch;
        while ((ch = r.read()) != -1) {
            if (!Character.isWhitespace(ch)) return false;
        }
        return true;
    }
}

// usage
if (Files.exists(p) && isWhitespaceOnly(p)) {
    Files.delete(p);
}

Practical tips: always close streams before deleting; handle the boolean/exception so you know when a delete actually failed; and be aware there is a tiny race between "check size" and "delete" if another process writes to the file in between.

Aside from your see the API docs for the File class and see if you can combine the length and delete methods.

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

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.

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.