Creating & Writing Text Files in Java (Part 6)
Pre-requisites:- Exception Handling , Reading Files Creating a Text File A text file can be created by many ways , here I will try to show the most used ways to create a text file. Firstly , we will learn to create a text file by using File class which is by far the most popular way to create a text file. First we have to create an instance of File class , then we have to call a method .createNewFile( ) which is a boolean i.e will return true or false. Also this method is suspicious of throwing an IOException so we have to catch it using a try-catch block. If file is created the .createNewFile( ) returns true if file is created or if file name mentioned exists previously in any form returns a false value. import java.io.File; import java.io.IOException; public class MyClass { public static void main(String[] args) { File file = new File(" example.txt "); //this will create a text file named example try { ...