旗下导航:搜·么
当前位置:网站首页 > JAVA教程 > 正文

java中怎样删除文件和目次【JAVA教程】,java,删除,文件,目录

作者:搜教程发布时间:2019-11-27分类:JAVA教程浏览:27评论:0


导读:delete()要领可用于删除文件或空目次/文件夹,假如文件被删除,delete()要领返回true,假如文件不存在则返回false。假如删除目次,delete()要领搜检...

delete()要领可用于删除文件或空目次/文件夹,假如文件被删除,delete()要领返回true,假如文件不存在则返回false。

假如删除目次,delete()要领搜检目次是不是为空,假如目次为空,则删除目次,不然delete()要领不会实行任何操纵并返回false,所以在这类情况下,必需递归删除一切文件,然后再删除目次。

删除非空目次的另一种要领是运用Files.walkFileTree()要领,在这类要领中,能够逐一处置惩罚一切文件,并在单个文件对象上挪用delete()要领。

删除文件示例:

package com.journaldev.files;import java.io.File;public class DeleteFileJava {
    /**
     * 此类显现如安在Java中删除文件
     * @param args
     */
    public static void main(String[] args) {
        // 带途径的文件名
        File file = new File("tmp/tmp2/file.txt");
        if(file.delete()){
            System.out.println("tmp/tmp2/file.txt File deleted");
        }else System.out.println("File tmp/tmp2/file.txt doesn't exist");
        // 只提供文件名称
        file = new File("file.txt");
        if(file.delete()){
            System.out.println("file.txt File deleted from Project root directory");
        }else System.out.println("File file.txt doesn't exist in the project root directory");
        //relative path
        file = new File("temp/file.txt");
        if(file.delete()){
            System.out.println("temp/file.txt File deleted from Project root directory");
        }else System.out.println("File temp/file.txt doesn't exist in the project root directory");
        // 删除空的目次
        file = new File("temp");
        if(file.delete()){
            System.out.println("temp directory deleted from Project root directory");
        }else System.out.println("temp directory doesn't exist or not empty in the project root directory");
        // 试图删除非空目次
        file = new File("D:/user/maxsu/project");
        if(file.delete()){
            System.out.println("D:/user/maxsu/project directory deleted from Project root directory");
        }else System.out.println("D:/user/maxsu/project directory doesn't exist or not empty");
    }}

注重:与createNewFile()差别,delete要领不会抛出IOException非常。

下面是一个简朴的顺序,演示怎样删除非空目次,假如目次包括有文件,删除目次不会胜利。

删除目次示例:

import java.io.File;public class JavaDeleteDirectory {
    public static void main(String[] args) {
        File dir = new File("D:/worksp/java/log");
        if(dir.isDirectory() == false) {
            System.out.println("Not a directory. Do nothing");
            return;
        }
        File[] listFiles = dir.listFiles();
        for(File file : listFiles){
            System.out.println("Deleting "+file.getName());
            file.delete();
        }
        //如今目次为空,所以能够删除它
        System.out.println("Deleting Directory. Success = "+dir.delete());
    }}

递归删除目次示例:

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class JavaDeleteDirectoryRecursively {
    public static void main(String[] args) throws IOException {
        Path directory = Paths.get("D:/worksp/maxsu/log");
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
           @Override
           public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
               Files.delete(file); // 有用,由于它始终是一个文件
               return FileVisitResult.CONTINUE;
           }
           @Override
           public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
               Files.delete(dir); //这将起作用,由于目次中的文件已被删除
               return FileVisitResult.CONTINUE;
           }
        });
    }}

引荐教程:java入门教程

以上就是java中怎样删除文件和目次的细致内容,更多请关注ki4网别的相干文章!

标签:java删除文件目录


欢迎 发表评论: