Delete all files in directory (but not directory) - one liner solution

前端 未结 11 1196
不思量自难忘°
不思量自难忘° 2020-12-12 11:38

I want to delete all files inside ABC directory.

When I tried with FileUtils.deleteDirectory(new File(\"C:/test/ABC/\")); it also deletes folder ABC.

相关标签:
11条回答
  • 2020-12-12 11:40

    I think this will work (based on NonlinearFruit previous answer):

    Files.walk(Paths.get("C:/test/ABC/"))
                    .sorted(Comparator.reverseOrder())
                    .map(Path::toFile)
                    .filter(item -> !item.getPath().equals("C:/test/ABC/"))
                    .forEach(File::delete);
    

    Cheers!

    0 讨论(0)
  • 2020-12-12 11:43
    package com;
    import java.io.File;
    public class Delete {
        public static void main(String[] args) {
    
            String files; 
            File file = new File("D:\\del\\yc\\gh");
            File[] listOfFiles = file.listFiles(); 
            for (int i = 0; i < listOfFiles.length; i++) 
            {
                if (listOfFiles[i].isFile()) 
                {
                    files = listOfFiles[i].getName();
                    System.out.println(files);
                    if(!files.equalsIgnoreCase("Scan.pdf"))
                    {
                        boolean issuccess=new File(listOfFiles[i].toString()).delete();
                        System.err.println("Deletion Success "+issuccess);
                    }
                }
            }
        }
    }
    

    If you want to delete all files remove

    if(!files.equalsIgnoreCase("Scan.pdf"))
    

    statement it will work.

    0 讨论(0)
  • 2020-12-12 11:45
    import org.apache.commons.io.FileUtils;
    
    FileUtils.cleanDirectory(directory); 
    

    There is this method available in the same file. This will also recursively deletes all sub-folders and files under them.

    Docs: org.apache.commons.io.FileUtils.cleanDirectory

    0 讨论(0)
  • 2020-12-12 11:46

    Do you mean like?

    for(File file: dir.listFiles()) 
        if (!file.isDirectory()) 
            file.delete();
    

    This will only delete files, not directories.

    0 讨论(0)
  • 2020-12-12 11:48

    Java 8 Stream

    This deletes only files from ABC (sub-directories are untouched):

    Arrays.stream(new File("C:/test/ABC/").listFiles()).forEach(File::delete);
    

    This deletes only files from ABC (and sub-directories):

    Files.walk(Paths.get("C:/test/ABC/"))
                    .filter(Files::isRegularFile)
                    .map(Path::toFile)
                    .forEach(File::delete);
    

    ^ This version requires handling the IOException

    0 讨论(0)
  • 2020-12-12 11:48

    Or to use this in Java 8:

    try {
      Files.newDirectoryStream( directory ).forEach( file -> {
        try { Files.delete( file ); }
        catch ( IOException e ) { throw new UncheckedIOException(e); }
      } );
    }
    catch ( IOException e ) {
      e.printStackTrace();
    }
    

    It's a pity the exception handling is so bulky, otherwise it would be a one-liner ...

    0 讨论(0)
提交回复
热议问题