Create File in Java



In java programming, To create the file very simple. Create the file object in try block, The try catch block used to handling the Exception.To refer the below code for how to create the file.








Example
import java.io.*;
public class Main
{
      public static void main(String[] args)
      {
            try 
            {
                  File file = new File("file_name.txt");

                  if (file.createNewFile()) 
                  {
                        System.out.println("File created.." );
                  } 

                  else 
                  {
                        System.out.println("File already exists.");
                  }
            } 
            
            catch (Exception e) 
            {
                  return;
            }
      } 
}



Output



File  created..



Download the code