How Create File Java

How Create File Java

In this java program you will see how to create file. To create new empty file instantiate File class with path name and call createNewFile() to create file as below:

package com.javahonk;

import java.io.File;

public class CreateFile {

	public static void main(String[] args) {

		try {

			File file = new File("C:\\JavaHonk\\File\\JavaHonk.txt");
			boolean fileCreated = file.createNewFile();
			if (fileCreated) {
				System.out.println("File created: "+file.getAbsolutePath());
			} else {
				System.out.println("File exists.");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
}
  • Output:

How Create File Java

For more information on File please refer oracle documentation here 

Leave a Reply

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