How to Create Text File using Java Program

Create Text File using Java

In this example, I am attaching Simple program about, How to Create Text File using Java. The example program has been tested and shared in the post.

package com.dineshkrish;

import java.util.Scanner;

 * @author Dinesh Krishnan

public class CreateTxtFile {

	// You can change the path accordingly
	private static final String DESTINATION_PATH = "output/";
	private static final String FILE_EXTENSION = ".txt";
	public static void main(String[] args) {
		Scanner scanner = new Scanner(;
		System.out.println("Enter the file name ?");
		String fileName =; // Reading File name from user
		String path = DESTINATION_PATH + fileName + FILE_EXTENSION;
		File file = new File(path);
		boolean status = false;
		try {
			// Creating the File
			status = file.createNewFile();
		} catch (IOException e) {
		if(status) {
			System.out.println("The file is created successfully...");
		} else {
			System.out.println("Can not able to create file...");


Enter the file name ?
The file is created successfully…

Generated File

Create Text File using Java


1. API JavaDocs
2. API JavaDocs

Hello, folks, I am a founder of I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.