Home > Back-end >  How to choose the specified file type?
How to choose the specified file type?

Time:09-24

I want to be a local file scanning software, and then I want to achieve file filter functions, such as only scan image type files, only scans the text documents, scan exe files, and so on, only the simplest approach is to specify file suffix, but it has great limitations, it is impossible for me to write all the possible suffix, it is too cumbersome! There is no other method can be implemented?

CodePudding user response:

refer to the original poster painting evening yan response:
I want to be a local file scanning software, and then I want to achieve file selection function, such as only scan image type files, only scans the text documents, scan exe files, and so on, only the simplest approach is to specify file suffix, but it has great limitations, it is impossible for me to write all the possible suffix, it is too cumbersome! There is no other method can be implemented?


Enum documents enumerated using understanding once, put so you need to scan the suffix in the enum, later you can just change the enum,

CodePudding user response:

Piss me off, on talent!!!!!

: don't want to write all extensions (suffix), you must know what are the files in the directory to scan the extension, which will show which, so the solution is:
1, all access to the specified directory file
2, put all the documents classified according to the extension
3, according to the classification of all extensions
4, to get a catalogue of all the files
 
/* * get dir all files in the directory, and save the files in the collection */
Private static void getAllFile (File dir, List Files) {
File[] fileArr=dir.listFiles();
If (fileArr==null | | fileArr. Length==0) return;
for (int i=0; i The File File=fileArr [I];
{if (file. IsFile ())
Files. The add (file);
} else {
GetAllFile (file, files);
}
}
}

/* * the files in the collection document classification, classified in a file extension */
Private static Map GetFileCategory (List Files) {
Return files. Stream (). Collect (Collectors. GroupingBy (file - & gt; {
String suffix;
The String fileName=file. The getName ();
If (fileName. The contains (". ")) {
Suffix=fileName. The substring (fileName. LastIndexOf (". ") + 1);
} else {
Suffix="";
}
Return the suffix;
}));
}


Public static void main (String [] args) throws the Exception {
The File dir=new File (" C: \ \ Users \ \ abczh \ \ Desktop ");
List Files=new ArrayList<> (a);
//get all dir directory file
GetAllFile (dir, files);
//type according to the extension, the key for the extension, the value for the corresponding file extension
Map FileCategoryMap=getFileCategory (files);
//print all extensions
FileCategoryMap. KeySet (). The forEach (System. Out: : println);
//print all. TXT file
FileCategoryMap. Get (" TXT "). The forEach (System. Out: : println);
}


If I solved your problem, answer bother junction post, give point integral, thank ^_^

CodePudding user response:

You have to collect the file type, there are many online materials can find, such as wikipedia, https://en.wikipedia.org/wiki/List_of_file_formats

CodePudding user response:

Don't want to through the extension to distinguish the file type, a few bytes before need to read the file content, to analyze various file types, such as the following image format
[0]=255 (v) and (v [1]=216)=='JPG';
[0]=66 (v) and (v [1]=77)=='. BMP ';
  • Related