Wai Loon II
Wai Loon II

Reputation: 259

problem with deleting files

private static void deletefile(String file) {

    int fileName = 500;
    int z;
    String[] File = new String[fileName];
    for (z = 0; z < fileName; z++) {
        File f1 = new File(
                "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc" + z
                        + ".txt");
        boolean success = f1.delete();
        if (!success) {
            System.out.println("Deletion failed.");
            System.exit(0);
        } else {
            System.out.println("File deleted.");
        }
    }
}

public static void main(String[] args) throws IOException {
    switch (args.length) {
    case 0:
        System.out.println("File has not mentioned.");
        System.exit(0);
    case 1:
        deletefile(args[0]);
        System.exit(0);
    default:
        System.out.println("Multiple files are not allow.");
        System.exit(0);

hi, this is my code for attempting to delete a certain files in java. It prints out file has not mentioned.i was trying to delete a set of txt files in a certain folder. The program should continue with the next file once a file is missing. Can anyone point out my mistake ? thanks..

Upvotes: 0

Views: 334

Answers (3)

Giannis
Giannis

Reputation: 5516

You need to have some check or catch exception when you create a new file so it wont stop when the file is not found.

Upvotes: 0

Nishan
Nishan

Reputation: 2871

You have to specify the file name as command line argument when running your Java program.

java MyClass file_to_delete

Upvotes: 0

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116246

Apparently you did not pass any command line parameters to your program.

(Although even if you did, it is not used anywhere in deletefile() - your method attempts to delete a fixed set of files in a specific directory, and if any of these is missing or you don't have permissions to delete it, it exits with an error message.)

Upvotes: 2

Related Questions