Delete Files with same Prefix String using Java

前端 未结 11 1720
梦谈多话
梦谈多话 2020-12-16 09:11

I have around 500 text files inside a directory with a same prefix in their filename say dailyReport_.

The latter part of the file is the date of the fi

相关标签:
11条回答
  • 2020-12-16 10:02

    With Java 8:

    public static boolean deleteFilesForPathByPrefix(final String path, final String prefix) {
        boolean success = true;
        try (DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(Paths.get(path), prefix + "*")) {
            for (final Path newDirectoryStreamItem : newDirectoryStream) {
                Files.delete(newDirectoryStreamItem);
            }
        } catch (final Exception e) {
            success = false;
            e.printStackTrace();
        }
        return success;
    }
    

    Simple version:

    public static void deleteFilesForPathByPrefix(final Path path, final String prefix) {
        try (DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(path, prefix + "*")) {
            for (final Path newDirectoryStreamItem : newDirectoryStream) {
                Files.delete(newDirectoryStreamItem);
            }
        } catch (final Exception e) { // empty
        }
    }
    

    Modify the Path/String argument as needed. You can even convert between File and Path. Path is preferred for Java >= 8.

    0 讨论(0)
  • 2020-12-16 10:05

    I agree with BegemoT.

    However, just one optimization: If you need a simple FilenameFilter, there is a class in the Google packages. So, in this case you do not even have to create your own anonymous class.

    import com.google.common.io.PatternFilenameFilter;
    
    final File folder = ...
    final File[] files = folder.listFiles(new PatternFilenameFilter("dailyReport_08.*\\.txt"));
    
    // loop through the files
    for ( final File file : files ) {
        if ( !file.delete() ) {
            System.err.println( "Can't remove " + file.getAbsolutePath() );
        }
    }
    

    Enjoy !

    0 讨论(0)
  • 2020-12-16 10:07

    Have a look at Apache FileUtils which offers many handy file manipulations.

    0 讨论(0)
  • 2020-12-16 10:08

    No, you can't. Java is rather low-level language -- comparing with shell-script -- so things like this must be done more explicetly. You should search for files with required mask with folder.listFiles(FilenameFilter), and iterate through returned array deleting each entry. Like this:

    final File folder = ...
    final File[] files = folder.listFiles( new FilenameFilter() {
        @Override
        public boolean accept( final File dir,
                               final String name ) {
            return name.matches( "dailyReport_08.*\\.txt" );
        }
    } );
    for ( final File file : files ) {
        if ( !file.delete() ) {
            System.err.println( "Can't remove " + file.getAbsolutePath() );
        }
    }
    
    0 讨论(0)
  • 2020-12-16 10:12

    Java 8 :

    final File downloadDirectory = new File("directoryPath");   
        final File[] files = downloadDirectory.listFiles( (dir,name) -> name.matches("dailyReport_.*?" ));
        Arrays.asList(files).stream().forEach(File::delete)
    
    0 讨论(0)
提交回复
热议问题