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();
}
}
}
Purpose
This program demonstrates how to:
- Create a new file in Java using the
File
class and itscreateNewFile()
method. - Handle exceptions using
try-catch
for robust error management.
1. Import Statements
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
java.io.File
: Provides theFile
class for file and directory manipulation, including creating, deleting, and checking file attributes.java.io.IOException
: Exception that is thrown when there are input/output-related issues (e.g., failure to create a file).java.util.Scanner
: Enables user input from the console.
2. Class Definition
public class FileCreate
- Defines the class
FileCreate
.
3. Main Method
public static void main(String[] args)
- The program's entry point.
4. Get User Input for File Name
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the file: ");
String file_name = sc.next();
- A
Scanner
object (sc
) is used to read the file name from the user. - The
next()
method reads a single token (a string without spaces).
5. Create a File
Object
File src_file = new File(file_name);
- A
File
object is created with the name provided by the user. - The
File
object represents the path to a file or directory.
6. Create the File
boolean flag = src_file.createNewFile();
- The
createNewFile()
method:- Creates a new, empty file if it does not already exist.
- Returns
true
if the file is created successfully, orfalse
if the file already exists. - Throws
IOException
if the file cannot be created due to an error (e.g., invalid path or lack of permissions).
7. Check Creation Status
if(flag)
{
System.out.println("File created successfully with name : " + file_name);
}
else
{
System.out.println("Unable to create file");
}
- If
flag
istrue
, the file was successfully created, and a success message is printed. - If
flag
isfalse
, the file already exists, or the operation failed.
8. Handle Exceptions
catch(IOException e)
{
System.out.println("Exception while creating file");
e.printStackTrace();
}
- IOException: Catches errors related to file creation, such as invalid file names or permission issues.
e.printStackTrace()
: Prints the stack trace for debugging.
Key Concepts
-
File
Class:- The
File
class is used to represent file and directory paths. - Common methods:
createNewFile()
: Creates a new file.exists()
: Checks if the file already exists.delete()
: Deletes the file.
- The
-
Error Handling:
IOException
is handled using atry-catch
block to ensure the program does not crash.
-
User Input:
- The program dynamically takes the file name from the user instead of hardcoding it.
Test Cases
Case 1: File Created Successfully
Enter the name of the file: example.txt
File created successfully with name : example.txt
Case 2: File Already Exists
Enter the name of the file: example.txt
Unable to create file
Case 3: Exception Occurs
Enter the name of the file: invalid?name
Exception while creating file
java.io.IOException: Invalid file path
at java.io.File.createNewFile(File.java:XYZ)
at FileCreate.main(FileCreate.java:XX)
Output
Enter the name of the file: source.txt
File created successfully with name : source.txt