File in Java Exclude system files from lists()

I'm in Java io. Use method file in file Listfiles() gets the list of files, but it returns some system files, such as (. Sys, etc.) I need to exclude all system related files (windows, Linux, MAC) when returning to the list Can anyone solve my problem?

Solution

I will implement a simple filefilter, whose logic is used to determine whether the file is a system file, and use its instance in the way of Alex r shown in his answer Things like this (rule a is for demonstration purposes only!):

public class IgnoreSystemFileFilter implements FileFilter {

   Set<String> systemFileNames = new HashSet<String>(Arrays.asList("sys","etc"));

   @Override
   public boolean accept(File aFile) {

     // in my scenario: each hidden file starting with a dot is a "system file"
     if (aFile.getName().startsWith(".") && aFile.isHidden()) {
       return false;
     }

     // exclude kNown system files
     if (systemFileNames.contains(aFile.getName()) {
       return false;
     }

     // more rules / other rules

     // no rule matched,so this is not a system file
     return true;
 }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>