need
A lot of files (such as pictures) are piled up in a directory, and their naming rules follow a certain format, and it is difficult to manage when mixed together. It needs to be automatically sorted through code. Automatically group image files based on file name prefix, creating a folder for each group to archive.
background
A batch of picture files with the following naming format:
A_20241021
A_20241021
A_20241022
A_20241023
The previous parts are consistent. Files with the same prefix belong to the same group. Based on this prefix, divide them into their respective "folders".
Analytical ideas
Extract file name prefix: separate with -number, remove the suffix name, and take the first half as the basis for grouping.
Use Map to store grouping results: key is the prefix and value is the corresponding file list.
Output or move to the corresponding folder.
Implement code
Here is the complete sample code, focusing on how to extract prefixes and group them:
import .*; public class FileGrouper { public static void main(String[] args) { // List of example file names List<String> fileNames = ( "A_20241021", "A_20241021", "A_20241022", "A_20241023" ); //Storing the grouped file name Map<String, List<String>> groupedFiles = new HashMap<>(); for (String fileName : fileNames) { // Extract the prefix part as key String prefix = getPrefix(fileName); // Add to the corresponding group (prefix, k -> new ArrayList<>()).add(fileName); } // Output grouping results for (<String, List<String>> entry : ()) { ("Folder: " + ()); for (String file : ()) { (" " + file); } } } /** * Get the file name prefix (remove the `-number` and extension) */ private static String getPrefix(String fileName) { int lastDashIndex = ('-'); if (lastDashIndex != -1) { return (0, lastDashIndex); } return fileName; } }
Output result
After running, you will get the following output:
Folder: A_20241021
A_20241021
A_20241021
Folder: A_20241022
A_20241022
Folder: A_20241023
A_20241023
The grouping was successful and a "folder name" was generated for each group.
Knowledge extension
Java Find files based on file name prefix
In Java, we can find files by prefixing the file name. This process is mainly divided into the following steps: reading the file directory, filtering the file name, matching the prefix, and finding the target file.
Detailed steps
Step 1: Read the file directory
First, we need to get a list of all file names in the target folder.
// Specify the destination folder pathFile folder = new File("path/to/folder"); // Get all files in the target folderFile[] listOfFiles = ();
Step 2: Filter the file name
Next, we need to filter out the target file name.
List<String> fileNames = new ArrayList<>(); for (File file : listOfFiles) { if (()) { (()); } }
Step 3: Match the prefix
Then, we need to match the file name prefix and find the target file.
String targetPrefix = "prefix"; String targetFile = null; for (String fileName : fileNames) { if ((targetPrefix)) { targetFile = fileName; break; } }
Step 4: Find the target file
Finally, we can find the target file based on the target file name.
File file = new File(() + + targetFile); // Perform subsequent operations, such as reading file content
This is the article about how Java automatically grouped image files based on file name prefix. For more related contents of Java automatically grouped image, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!