Java Program to Create a File
Program
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FileCreate
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
File src_file = null;
try
{
System.out.print("Enter the name of the file: ");
String file_name = sc.next();
src_file = new File(file_name);
boolean flag = src_file.createNewFile();
if(flag)
{
System.out.println("File created successfully with name : " + file_name);
}
else
{
System.out.println("Unable to create file");
}
}
catch(IOException e)
{
System.out.println("Exception while creating file");
e.printStackTrace();
}
}
}
Output
Enter the name of the file: source.txt
File created successfully with name : source.txt