How to create a simple text file in Java

How to create a simple text file in Java

17th October 2017 9,418 views




For this tutiorial, we are going to learn how to create a simple text file. Before learning this lesson, it is assumed that you are not entirely new to Java programming. At least you should know the concepts of classes, exceptions and the basic use of Java language element.


Tutorial rating: intermediate



Java provides one of the easiest ways to create a text file. I remember when I was learning C++, I knew how long it took me to even attempt creating a text file in C++ because of how intimdating the codes looked. But one of Java's aim as you know is to simplify things. To create a text file in Java, all you need to know about are two Java in-built classes: File, FileWriter and PrintWriter.



File: this file creates the needed file, and specifies its location on the system. The File class is an interesting class that contains methods for manipulating on files and directories.



FileWriter: is used to write character-oriented data to a file. It is character-oriented class which is used for file handling in java. It is a lot easier than using the FileOutputStream class where you will need to convert string into byte array and blah blah blah...



Printwriter: this class is used to print formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream.



From the study of the above classes, you will see why i use them and what each of them does; and they are quite easy and straightforward to use. So enough said already, Here is the program below which will create a text file named "test.txt" on the same directory where the source file is saved. For the sake of convenience, you might want to save your source code on the desktop to quickly observe the created file. Thank you



Program



import java.io.File;

import java.io.FileWriter;

import java.io.PrintWriter;

import java.util.Scanner;

import java.io.IOException;



public class FileCreator {

    

    public static void main(String [] args){

    

        Scanner input = new Scanner(System.in);

        File file = new File("test.txt");

        PrintWriter printer;

        FileWriter fwr;

        

        System.out.println("enter some text. press [ENTER] when you are done");

        String data = input.nextLine();

        

        try {

        

            fwr = new FileWriter(file);

            printer = new PrintWriter(fwr);



            printer.println(data);

            printer.close();



            System.out.println( "File successfully written");

            

        } catch (IOException e) {

            

            e.printStackTrace();

        }

        

    }





}



So that's it. Try it and give me feedback. Keep a date with me next wednesday!







Comments

We would like to hear from you to know how you have benefitted from this article. Please feel free to share a comment. (You will need to be logged into your Facebook account to do this)