Create, Rename & Delete a file in Java - Java File Handling

In our previous discussions we came across a number of Java Implementation and Examples, in this particular blog we will see 'How to create a file in Java', 'How to rename a file in Java' & 'How to delete a file in Java'. We will use 'java.io.File' class to perform the create, rename and delete operations.


Project Structure

This is a simple java project with all code file to perform the operations, create a folder with name 'files' to store files and perform operations on.


How to create a file in Java

To create a file in java, 'File.createNewFile()' method is used, it returns a boolean value, true if the file is created successfully false otherwise. See the code below :

\src\com\beingjavaguys\sample\CreateFile.java

package com.beingjavaguys.sample;  
  
/** 
 * @author Nagesh Chauhan 
 */  
import java.io.File;  
import java.io.IOException;  
  
public class CreateFile {  
 public void createFile() {  
  String fileName = "newFile.xml";  
  File file = new File("files/newFile.xml");  
  
  try {  
   if (file.createNewFile()) {  
    System.out.println("File named " + fileName  
      + " created successfully !");  
   } else {  
    System.out.println("File with name " + fileName  
      + " already exixts !");  
   }  
  } catch (IOException e) {  
   e.printStackTrace();  
  }  
 }  
}  


How to rename a file in Java

To rename a file in java, 'File.renameTo(newFileName)' method is used, it returns a boolean value, true if the file is renamed successfully false otherwise. See the code below :

\src\com\beingjavaguys\sample\RenameFile.java

package com.beingjavaguys.sample;  
  
/** 
 * @author Nagesh Chauhan 
 */  
import java.io.File;  
import java.io.IOException;  
  
public class RenameFile {  
 public void renameFile() {  
  File oldFileName = new File("files/newFile.xml");  
  File newFileName = new File("files/newFileName.xml");  
  
  try {  
   if (oldFileName.renameTo(newFileName)) {  
    System.out.println("File renamed successfull !");  
   } else {  
    System.out.println("File rename operation failed !");  
   }  
  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
 }  
}  



How to Delete a file in Java

To delete a file in java, 'File.delete()' method is used, it returns a boolean value, true if the file is deleted successfully false otherwise. See the code below :

\src\com\beingjavaguys\sample\DeleteFile.java

package com.beingjavaguys.sample;  
  
/** 
 * @author Nagesh Chauhan 
 */  
import java.io.File;  
  
public class DeleteFile {  
  
 public void deleteFile() {  
  try {  
   File fileToDelete = new File("files/newFileName.xml");  
  
   if (fileToDelete.delete()) {  
    System.out.println("File deleted successfully !");  
   } else {  
    System.out.println("File delete operation failed !");  
   }  
  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
 }  
}  



Implementation class code

This is simple java class having a main method to start execution, implementation code goes here. Please make sure to use each operation one by one to see the implementation.

\src\com\beingjavaguys\sample\FileHandlingMain.java


package com.beingjavaguys.sample;  
  
/** 
 * @author Nagesh Chauhan 
 */  
  
public class FileHandlingMain {  
  
 public static void main(String[] args) {  
  
  /* use one operation at a time to check the functionality */  
  
  // creating a file  
  CreateFile createFile = new CreateFile();  
  createFile.createFile();  
  
  // rename a file  
  RenameFile renameFile = new RenameFile();  
  // renameFile.renameFile();  
  
  // delete a file  
  DeleteFile deleteFile = new DeleteFile();  
  // deleteFile.deleteFile();  
 }  
  
}  


In this particular blog we came across 'How to create a file in Java', 'How to rename a file in Java' & 'How to delete a file in Java'. In upcoming blogs we will see more about 'Java Handling' and Other Implementations in Java.