How to rename a file from a directory or folder in java
The method renameTo(File file) in java.io.File is used to rename a file or folder . If the directory or folder not having the specified file , the operation will fails.
The operation of rename a file might not be able to move a file from one file system to another.
It returns true if the file name renamed to specified name , false otherwise.
Example rename a file:
public static void main(String args)
File fileToRename = new File("C:\\userfile\\file.txt");
File nameToRename = new File("C:\\userfile\\renamefile.txt");
System.out.println("Actual name is : "+fileToRename.getName());
System.out.println("File renamed successfully");
System.out.println("not renamed successfully");
System.out.println("Renamed file is : "+nameToRename.getName());
Actual name is : file.txt
File renamed successfully
Renamed file is : renamefile.txt
Rename might not be succeed if a file with the destination abstract path name (same name) already exists .
|Related Posts :|
|How to Connect to Oracle from Liferay portlet ( Java Liferay and Oracle integration )|
|How to Connect Liferay 7 to PostgreSQL ( Java Liferay portlet and PostgreSQL integration )|
|How to call C# .Net WCF web service in java SOAP client ( Visual Studio 2013 )|
|How to call PHP RESTful web service in java client|
|How to call java SOAP web service in php ( php web service client )|
|Solution to SSLHandshakeException (skip the certificate for Java and LDAP bind)|
|Java RESTful web service example in maven using JAX-RS|
|RESTful web service example in java using JAX-RS|