How to create new file in Java?

Below is the java code to create a new file using java.io.Java class

/****************************************************************************************
 * Created on 08-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.io;

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

/**
 * Created by Vigilance India.
 * User: https://kodehelp.com
 * Date: Aug 4, 2011
 */
public class CreateNewFile {
    public static void main(String args[]){
        try{
         File file = new File("/demo.txt");
         if(file.createNewFile()){
             System.out.println("New File Created");
         }else{
             System.out.println("File already exists");
         }
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
}

2 comments

  1. Rajiv says:

    There is another way of reading text file in Java by using Scanner. see http://javarevisited.blogspot.sg/2011/12/read-and-write-text-file-java.html for more details.

  2. Rajiv says:

    There is another way of reading text file in Java by using Scanner. see http://javarevisited.blogspot.sg/2011/12/read-and-write-text-file-java.html for more details.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.