Create File using Java IO (With Examples)

How to Create File using Java IO?

Problem Statement

How to create file using Java IO?

Create File using Java IO

Create File using Java IO

Description

To create file using Java IO, we can use api – File.createNewFile(). This method returns a boolean value true if the file is created successful; false if the file is already exists. If the path is not found it throws the IOException to indicate operation failed.

——————————————————————————

Code

package com.stixs.java.io;
import java.io.File;
import java.io.IOException;
public class STXCreateNewFile 
{
    public static void main(String[] args) throws IOException 
   {
       // create a new file instance by passing pathname(String)
       File file = new File("C:\\technostixs\\java\\samplefile.txt"); 
 
        // Use Java IO method 'createNewFile()' of class 'File'
        if(file.createNewFile()) 
        { 
            System.out.println("STXCreateNewFile: New File created!");
        }
        else 
        {
             System.out.println("STXCreateNewFile: Another file with same name already exists!");
        }
    }
}

Snapshot

Create File using Java IO

Create File using Java IO

——————————————————————————–

Output:

Create a folder as given in program: C:\technostixs\java. Then you will see the below output when executed for the first time (when no file with name samplefile.txt exist)

STXCreateNewFile: New File created!
Create File using Java IO

Output: Create File using Java IO

 

When file with same name samplefile.txt exist

STXCreateNewFile: Another file with same name already exists!

Exceptional output

If the file path (C:\technostixs\java) you have given does not exist, you will find below exception:

Exception in thread "main" java.io.IOException: The system cannot find the path specified
       at java.io.WinNTFileSystem.createFileExclusively(Native Method)
       at java.io.File.createNewFile(Unknown Source)
       at com.stixs.java.io.STXCreateNewFile.main(STXCreateNewFile.java:14)

 

Create File using Java IO - IO Exception

Create File using Java IO – IO Exception

——————————————————————————–

The Java API used to create file using Java IO:

Create File using Java IO

Create File using Java IO

 

Create File using Java IO - Java File Class, createNewFile()

Create File using Java IO – Java File Class, createNewFile()

Reference

Refer below path to get the complete details about Java IO File class.

http://docs.oracle.com/javase/8/docs/api/java/io/File.html

——————————————————————————–

You may also like this

>>>Java IO – Code to get all file names in given directory

>>>Java Util – Date example with formatter

>>>Java Utils – Code to finding Today’s Date

——————————————————————————–

About TechnoStixs.com

TechnoStixs

TechnoStixs

TechnoStixs.com is a dedicated weblog for Java/J2EE and web developers. We take pride in our work. Every publication is carefully analyzed, written and tested to ease the understanding of the topic or subject.

We cover Java Core Technologies, eCommerce, J2EE Frameworks, Web Service, Build Tools, Unit Test Frameworks and Others.

1,116 total views, 1 views today

Add comment