Get the file name (excluding the file suffix) and rename the file in java, and rename the file in java
Get the file name (excluding the suffix)
originalFileName.substring(0, originalFileName.lastIndexOf("."))
OriginalFileName indicates the file.
File rename
Public void renameFile (String file, String toFile) {File toBeRenamed = new File (file); // check whether the file to be renamed exists and whether it is a File if (! ToBeRenamed. exists () | toBeRenamed. isDirectory () {System. out. println ("File does not exist:" + file); return;} File newFile = new File (toFile); // modify the File name if (toBeRenamed. renameTo (newFile) {System. out. println ("File has been renamed. ");} else {System. out. println ("Error renmaing file ");}}
Call the above method to rename the file.
renameFile("C:\\temp\\file1.txt", "C:\\temp\\file2.txt");
Java obtains the file name and extracts the local string split file name and Suffix from the string.
String fileName = name. substring (0, name. lastindexOf (".");
The Word document and TXT document cannot be renamed. Once renamed, the system will prompt that the file name cannot contain any of the following characters.
That's because your file name contains these special characters. Instead of duplicate names.
Try it like this. Create a try Word document and TXT document.