Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Tuesday, 12 May 2015

How to Create File using Java

Following example shows hoe to create file using File() constructor and file.createNewFile() method in Java

createNewFile
public boolean createNewFile() throws
Atomically creates a new, empty file named by this abstract pathname if
and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.
Returns:
true if the named file does not exist and was successfully created; false if the named file already exists.
Throws:
IOException - If an I/O error occurred.
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the file.

Sample Program
CreateFile.java
01.package com.javatutorialcorner.file;
02. 
03.import java.io.File;
04.import java.io.IOException;
05. 
06.public class CreateFile {
07. 
08. public static void main(String[] args) {
09.  String path = "/home/annamalai/workspace/File/jtc.txt";
10.  try {
11.   File jtcFile = new File(path);
12.   if (!jtcFile.exists()) {
13.    jtcFile.createNewFile();
14.    System.out.println("File Created Successfully!");
15.   } else {
16.    System.out.println("Error, file already exists.");
17.   }
18.  } catch (IOException ioe) {
19.   ioe.printStackTrace();
20.  }
21. 
22. }
23. 
24.}

Result
The above code will create new file if file not exist in specified directory.
File Created Successfully!

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to write file using FileOutputStream in JavaIn this tutorials we are going to see how to write file using FileOutputStream in Java. 1.Create project called JavaMisc. 2.Create package called com.javatutorialscorner.io Create java class called FileWriter under c… Read More
  • Read File using BufferedInputStream In this tutorials we are going to see how to read file using BufferedInputStream. Create project called JavaMisc. Create package called com.javatutorialscorner.io  Create java class called FileReaderExample… Read More
  • How to Create File using Java Following example shows hoe to create file using File() constructor and file.createNewFile() method in Java createNewFile public boolean createNewFile() throws Atomically creates a new, empty file named by this abstract pa… Read More
  • How to check file existence in Java Following example shows how to check file existence using file.exists() method in Java. existspublic boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. Returns:    … Read More
  • Reading the file using FileInputStreamIn this tutorials we are going to see how to read file using FileInputStream. Create project called JavaMisc. Create package called com.javatutorialscorner.io Create java class called FileReader under com.javatutorialscorner.… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to Create File using Java Rating: 5 Reviewed By: eHowToNow