开发者

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 开发者_StackOverflowstatic 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..


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


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

java MyClass file_to_delete


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜