Get the file name (excluding the file suffix) and rename the file in java, and rename the file in java

Source: Internet
Author: User

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.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.