reader www.codegeek.io

How to Create a basic Text file editor in java

Hey, everyone! Today, I am going to show you guys, How to make a text file editor using Java. So without wasting any further time, let’s get Started with it. We will make 3 programs today, First one will be used For Writing to a text file and Second one will be used Reading from a text file and The third One will be able to Read and Write to the file the same time.

 

Program for Writing data to a text File :

import java.io.FileWriter;

public class Main {

    public static void main(String[] args) {

        // Try block so we can catch any errors if they occur
        try {

            // FileWriter is used to write on a existing file or to create a new one if it don't already exist 
            
            FileWriter writer = new FileWriter("D:\\Examples\\Example.txt");
            
            // writer.write is used to write the string u passed on that file u selected first 
            writer.write("hey");
            
            // Writer.close tells the writer that u r done writing on the file so it can safely exit it and save it 
            writer.close();
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

Program for reading data from a text File :

import java.io.FileReader;
import java.io.Reader;

public class Main {

    public static void main(String[] args) {

        // Try block so we can catch any errors if they occur
        try {

            // Calling Reader as reader, Reader is basically used for reading character streams and File reader is used to Locate and
            // read the file by converting them from characters to numbers
            Reader reader = new FileReader("D:\\Examples\\Example.txt");


            String Data = "";

            char charToStore;
            // reader.read basically get that character in numerical form
            int i = reader.read();

            // We r using while loop till we get a number which is -1 and -1 is always at the end of file, it won't appear before that no matter what
            // so this is the most efficient way to read the whole file
            while (i != -1) {

                // Converting that number we stored in Variable I to character so we can store it in a string
                charToStore = (char) i;

                // Storing character in String by appending them 
                Data += charToStore;
                
                // assigning value of next character to variable i
                i = reader.read();
            }
            
            System.out.println(Data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Program which can do both things:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("hey, enter write to write on file and read to Read the file");

        String choice = scanner.next();

        if (choice.equals("write") || choice.equals("Write")) {
            Writer();
        } else if (choice.equals("Read") || choice.equals("read")) {
            Reader();
        } else {
            System.out.println(" Please select a valid choice ");
        }

    }

    // Writer Function 
    public static void Writer() {
        // Try block so we can catch any errors if they occur
        try {

            // FileWriter is used to write on a existing file or to create a new one if it don't already exist

            FileWriter writer = new FileWriter("D:\\Examples\\Example.txt");

            // writer.write is used to write the string u passed on that file u selected first
            writer.write("hey");

            // Writer.close tells the writer that u r done writing on the file so it can safely exit it and save it
            writer.close();


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(" Error while writing to the file ");
        }
    }

    // Reader function 
    public static void Reader() {
        // Try block so we can catch any errors if they occur
        try {
            // Calling Reader as reader, Reader is basically used for reading character streams and File reader is used to Locate and
            // read the file by converting them from characters to numbers
            Reader reader = new FileReader("D:\\Examples\\Example.txt");
            // reader.read basically get that character in numerical form
            int i = reader.read();

            char charToStore;

            String Data = "";
            // We r using while loop till we get a number which is -1 and -1 is always at the end of file, it won't appear before that no matter what
            // so this is the most efficient way to  read the whole  file
            while (i != -1) {
                // Converting that number we stored in Variable I to character so we can store it in a string
                charToStore = (char) i;
                // Storing character in String by appending them 
                Data += charToStore;
                // assigning value of next character to variable i
                i = reader.read();
            }

            System.out.println(Data);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error while reading from the file");
        }
    }
}

PS: You can use scanner to enter the custom File path or to Enter custom text in a file. I will make a tutorial on how to make a full fledged Text editor in future and If we get enough likes on our Fb page, We will also start making video tutorials too. Thanks for reading 🙂 If you have any questions you can post them in the comments.

 

Leave a Reply