import java.io.File; import java.util.Scanner;

public class FileOperation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入文件路径:"); String filePath = scanner.nextLine(); File file = new File(filePath); while (!file.exists()) { System.out.println("文件不存在,请重新录入:"); filePath = scanner.nextLine(); file = new File(filePath); } System.out.println("文件名:' + file.getName()); System.out.println("文件大小:' + file.length() + " 字节"); System.out.println("是否可写:' + file.canWrite()); System.out.println("是否删除此文件?(是/否)"); String choice = scanner.nextLine(); if ("是".equals(choice)) { if (file.delete()) { System.out.println("删除成功"); } else { System.out.println("删除失败"); } } scanner.close(); } }


原文地址: https://www.cveoy.top/t/topic/ngFp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录