In this article we will see how to create a text file in java. In this example, we’ll use the PrintWriter class.

package com.javatechig;


public class CreateTextFile {

	public static void main(String[] args) {
		PrintWriter writer = null;
		try {
			/* Create a new file with UTF-8 encoding */
			writer = new PrintWriter("file1.txt", "UTF-8");

			/* Write content to file */
			writer.println("Hello, this is a binary file.");
			writer.println("Put your file contents here...");

			System.out.println("New File Created!");			
		} catch (FileNotFoundException e) {
		} catch (UnsupportedEncodingException e) {

This article is about: Core Java, Java Basics,


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (, DZone MVB and founder of

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.