How to create a file in Java

The File.createNewFile() method is used to create a file in Java, and return a boolean value : true if the file is created successful; false if the file is already exists or the operation failed.


package com.mkyong.file;

import java.io.File;
import java.io.IOException;

public class CreateFileExample 
{
    public static void main( String[] args )
    {	
    	try {
    		 
	      File file = new File("c:\\newfile.txt");
	      
	      if (file.createNewFile()){
	        System.out.println("File is created!");
	      }else{
	        System.out.println("File already exists.");
	      }
	      
    	} catch (IOException e) {
	      e.printStackTrace();
	}
    }
}

Reference

  1. http://java.sun.com/javase/6/docs/api/java/io/File.html

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
boris
Guest
boris

It doesn’t work. new File(String) throws a NullPointerException which is not being caught in this example.

Dattz
Guest
Dattz

Only throws NullPointerException if the passed argument is null.

varinder dua
Guest
varinder dua

nice article

mike drew
Guest
mike drew

very simple example ,
i also learned a lot from this site thanks a lot
but i would like to say that some times such a tutorials are not enough until u don’t have clear knowledge of the technology , moreover i have taken classes for core java from
http://www.mkjit-solutions.com that training really helps me a lot

james
Guest
james
Rohit More
Guest
Rohit More

how to create file within project folder like webContent?

shreya
Guest
shreya

thank yu
it helped in my exam

john
Guest
john

I copied your codes on eclipse but file doesn’t create
Can you help me!!!!

NISHIT PATIRA
Guest
NISHIT PATIRA

If you get the message that says “A required privelage does not exist”, change the file path from C:\newfile.txt to some place else. That may solve the problem.

imran
Guest
imran

Can you tell me that if the user creates a file than again he want to create a new file again . by entering his own file url e.g newfile.txt …. please sir help me?

NISHIT PATIRA
Guest
NISHIT PATIRA

It is not possible as the file already exists. You can create a new file in a separate folder by changing the path.

Mubeen
Guest
Mubeen

Thanks it cleared most of my doubts, but can u tell me what is the purpose of this “file.createNewFile”.

prathmeshn
Guest
prathmeshn

If doesnt exist it creates new file

Guest
Guest
Guest

hiiiiiiiiiii

Oleg Derecha
Guest
Oleg Derecha

Thanks, it helped to overcome/solve problem FileNotFoundException in IntelliJ IDEA

Khair Ali Naqvi
Guest
Khair Ali Naqvi

Ya Ali Madad A.S

IT and Non IT Jobs
Guest
IT and Non IT Jobs

Very simple example for Java beginners.

owenbowen
Guest
owenbowen

i get an error saying access denied

amit
Guest
amit

Must Read

amit
Guest
amit

Good Tutorial

ryan
Guest
ryan

Hello mates, its fantastic article on the topic of cultureand
entirely defined, keep it up all the time.

raghu
Guest
raghu

showing command like do without try and catch

keren happuch
Guest
keren happuch

“enter the file name to be created” i want write the prg for this? if anyone know pls help

ravi.s
Guest
ravi.s

i’m great fan of your site

Pankaj
Guest
Pankaj
Racha Suman
Guest
Racha Suman

your doing very well for creating good JAVA Programmers by learning from ur Website sir,,,,,,,,
i likes ur website a lot and as well as u….
From Racha Suman, India

Tlhologelo
Guest
Tlhologelo

The tutorials would be good with documentation/comments there is really no beginner who can grasp this stuff all at once.

ashish
Guest
ashish

I want to know is this a static page or dynamically generated

trackback
Java I/O Tutorials | J2EE Web Development Tutorials

[…] Create a file […]