I am getting list of file using method File.listFiles()
in java.io.File
, but it returns some system files like (.sys
and etc
).. I am in need of excluding all system related files (Windows, Linux, Mac) while returning lists. Can any one solve my issue?
list() returns the array of files and directories in the directory defined by this abstract path name. The method returns null, if the abstract pathname does not denote a directory.
The List() method. This method returns a String array which contains the names of all the files and directories in the path represented by the current (File) object. Using this method, you can just print the names of the files and directories.
The method java. io. File. isHidden() is used to check whether the given file specified by the abstract path name is a hidden file in Java. This method returns true if the file specified by the abstract path name is hidden and false otherwise.
I'd implement a simple FileFilter
with the logic to determine, if a file is a system file or not and use an instance of it the way AlexR showed in his answer. Something like this (the rules a 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;
}
I don't think there is a general solution to this. For a start, operating systems such as Linux and MacOS don't have a clear notion of a "system file" or any obvious way to distinguish a system file from a non-system file.
I think your bet is to decide what you mean by a system file, and write your own code to filter them out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With