Origin

A utility class for file name operations, mainly for obtaining the main file name, extension, and other operations. It also cleans up invalid characters specifically for the Windows platform.

This utility class was originally part of FileUtil before version 5.4.1 and was later separated into the tool FileNameUtil.

Usage

  1. Obtaining the file name
File file = FileUtil.file("/opt/test.txt");

// "test.txt"
String name = FileNameUtil.getName(file);
  1. Obtaining the main file name and extension
File file = FileUtil.file("/opt/test.txt");

// "test"
String name = FileNameUtil.mainName(file);

// "txt"
String name = FileNameUtil.extName(file);

Note that the extension obtained here does not include the .. FileNameUtil.mainName is equivalent to FileNameUtil.getPrefix, and similarly, FileNameUtil.extName is equivalent to FileNameUtil.getSuffix. The two methods are kept to accommodate different user habits.