Why Java is the Perfect Tool for Automation?
Have you ever spent half a day renaming 200 files in a folder? Or manually copied data from a dozen Excel spreadsheets into a report? Sound familiar? Then this article is for you.
Java is not just a language for huge enterprise systems. It is a powerful, stable, and cross-platform tool that is excellent for automating routine tasks. Unlike scripting languages, Java programs run fast, are easy to maintain, and the vast ecosystem of libraries (Maven Central) allows you to solve virtually any task "out of the box."
In this article, we won't talk about theory. We'll break down 5 real-world Java automation scenarios with ready-to-use code that you can copy and adapt to your needs. Let's go!
1. Automating File System Operations (Renaming, Sorting, Cleaning)
The most common task is to organize files. Imagine: you have a folder with reports named report_2024_01_15.xlsx, report_2024_01_16.xlsx, and so on. You need to move them into folders by month. Doing it manually takes 15 minutes. Java takes 2 seconds.
Example: Automatic File Sorting by Date
import java.io.IOException;import java.nio.file.*;import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.util.regex.Matcher;import java.util.regex.Pattern;
public class FileSorter { public static void main(String[] args) throws IOException { Path sourceDir = Paths.get("C:/reports"); Pattern pattern = Pattern.compile("report_(\\\\d{4})_(\\\\d{2})_(\\\\d{2})\\\\.xlsx");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(sourceDir, "*.xlsx")) { for (Path file : stream) { Matcher matcher = pattern.matcher(file.getFileName().toString()); if (matcher.matches()) { String year = matcher.group(1); String month = matcher.group(2); Path targetDir = sourceDir.resolve(year).resolve(month); Files.createDirectories(targetDir); Files.move(file, targetDir.resolve(file.getFileName()), StandardCopyOption.REPLACE_EXISTING); System.out.println("Moved: " + file.getFileName()); } } } System.out.println("Done! Files sorted into folders."); }}What does this code do? It scans the folder, finds all files matching the pattern, extracts the year and month from the filename, creates nested folders, and moves the files. Simple and reliable.
You can easily adapt this code to your needs: change the pattern, add logging, or make it recursively traverse subfolders. This is the foundation for any task automation in Java related to files.
2. Parsing and Processing Excel Files (Apache POI)
Excel is the "favorite" tool of accountants, managers, and analysts. Often you need to collect data from 10 files into one, or conversely, split one large file into several. The Apache POI library is the de facto standard for working with Excel in Java.
Example: Reading Data from Excel and Generating a Summary Report
import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.HashMap;import java.util.Map;
public class ExcelAggregator { public static void main(String[] args) throws IOException { Map<String, Double> summary = new HashMap<>();
// Read three files String[] files = {"sales_q1.xlsx", "sales_q2.xlsx", "sales_q3.xlsx"}; for (String fileName : files) {