zoukankan      html  css  js  c++  java
  • 删除文件或目录

     1 package io.FileOp;
     2 import java.io.*;
     3 /**
     4  * 删除文件或者删除目录
     5  * @author www.tfgzs.net
     6  */
     7 public class Delete {
     8 
     9     public static void main(String[] args) {
    10         String pathString="E:/360Downloads/123456.txt"; //文件路径
    11         
    12         if (delete(pathString)) {
    13             System.out.println("删除成功!");
    14         }else {
    15             System.out.println("删除失败!");
    16         }
    17     }
    18     /**
    19      * 删除文件或者删除目录
    20      * @param filename 文件路径
    21      * @return 成功返回true失败返回false
    22      */
    23     public static boolean delete(String filename) {
    24         File file = new File(filename);
    25         if (!file.exists()){
    26             System.err.println("文件已经存在");
    27             return false;
    28         }
    29         if (!file.canWrite()){
    30             System.err.println("该文件受到保护");
    31             return false;
    32         }
    33         if(file.isDirectory()){
    34             String[] files = file.list();
    35             if (files.length > 0){
    36                 System.err.println("该目录下存在其他文件");
    37                 return false;
    38             }
    39         }
    40         return file.delete();
    41     }
    42 }
  • 相关阅读:
    vue-router的基本使用
    SQL Server加密存储过程的破解
    IIS绑定Active Directory账号自动登录网站的方法
    .Net Install类的Install、Commit等事件触发顺序
    正态分布公式
    HDU4417 Super Mario
    CodeChef
    Gym101630C Connections
    CF916C
    CF912D Fishes
  • 原文地址:https://www.cnblogs.com/tfgzs/p/3833227.html
Copyright © 2011-2022 走看看