2019-12-27-Java控制删除文件

package test01;

import java.io.File;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    String name = "";

    while(!name.equals("exit")) {

        System.out.println("请输入需要删除的文件名");

        @SuppressWarnings("resource")

        Scanner in = new Scanner(System.in);

        name = in.next();

        if(name.equals("exit"))break;

        String filePath = "E:\\Files\\"+name;

        File file = new File(filePath);

        if(file.exists()&&file.isFile()) {

            if(file.delete()) {

                System.out.println("删除成功!");

            }else {

                System.out.println("删除失败!");

            }

        }else

    {

    file.delete();

    System.out.println("文件不存在");

            }

        }

    }

}

你可能感兴趣的:(2019-12-27-Java控制删除文件)