Writing Notepad based on Java

Keywords: Java

Learned some basic knowledge of Java and wrote a super simple Notepad.

First of all, a large framework should be established. For example, Windows Notepad has the functions of creating a new file, opening a file, modifying a file, saving and so on. Without saying much, you can go directly to the code.

    private static String filePath;
    private static String message = "";
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("1: New file 2: open file 3: modify file 4: save 5: exit");
        //create,open,modify,save,exit
        while(true) {
            System.out.println("Please enter instructions:");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    createFile();
                    break;
                case 2:
                    openFile();
                    break;
                case 3:
                    modifyFile();
                    break;
                case 4:
                    saveFile();
                    break;
                case 5:
                    exit();
                    break;
                default:
                    System.out.println("The command you entered is wrong( one-5)");
                    break;
            }
        }
    }

I don't know how to describe some specific points to make them easy to understand. Anyway, after learning, I know how to write like this. I didn't search the Internet for how others wrote it. This should be the simplest.
Next, there are several small frameworks. I won't say much more. After running all the code, the operation is very simple. You should be able to start directly after reading it.
new file

private static void createFile() {
        message = "";
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the content and enter it when stopping\"stop\":");
        StringBuffer sBuffer = new StringBuffer();
        String inputMessage = "";
        while (!inputMessage.equals("stop")) {
            if (sBuffer.length()>0) {
                sBuffer.append("\r\n");
            }
            sBuffer.append(inputMessage);
            inputMessage = scanner.nextLine();
        }
        message = sBuffer.toString();
    }

Open file

private static void openFile() throws Exception {
        message = "";
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the location of the open file:");
        filePath = scanner.next();
        if (filePath != null && !filePath.endsWith(".txt")) {
            System.out.println("Please select a text file!");
            return;
        }
        FileReader in = new FileReader(filePath);
        char[]charArray = new char[1024];
        int len = 0;
        StringBuffer sb = new StringBuffer();
        while((len = in.read(charArray)) != -1) {
            sb.append(charArray);
        }
        message = sb.toString();
        System.out.println("Open file contents:"+"\r\n" + message);
        in.close();
    }

Modify file

private static void modifyFile() {
        if (message == "" && filePath ==null) {
            System.out.println("Please create a new file or open it first");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the content to be modified (in Chinese)\"Modified target text: modified text\")," + "Stop modifying, please enter\"stop\":");
        String inputMessage = "";
        while(!inputMessage.equals("stop")) {
            inputMessage = scanner.nextLine();
            if (inputMessage != null && inputMessage.length() > 0) {
                String[] modifyMessage = inputMessage.split(":");
                if (modifyMessage != null && modifyMessage.length > 1) {
                    message = message.replace(modifyMessage[0], modifyMessage[1]);
                }
            }
        }
        System.out.println("Modified content:" + "\r\n" + message);
    }

Save file

private static void saveFile() throws IOException {
        Scanner scanner = new Scanner(System.in);
        FileWriter out = null;
        if (filePath != null) {
            out = new FileWriter(filePath);
        }else {
            System.out.println("Please enter the absolute path to save the file:");
            String path = scanner.next();
            filePath = path;
            if (!filePath.toLowerCase().endsWith(".txt")) {
                filePath += ".txt";
            }
            out = new FileWriter(filePath);
        }
        out.write(message);
        out.close();
        message = "";
        filePath = null;
    }

sign out

private static void exit() {
        System.out.println("You have exited the system, thank you for using!");
        System.exit(0);
    }

Some methods used in the code must be imported into the package, and then the complete code.

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

public class Notepad {


    private static String filePath;
    private static String message = "";
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("1: New file 2: open file 3: modify file 4: save 5: exit");
        while(true) {
            System.out.println("Please enter instructions:");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    createFile();
                    break;
                case 2:
                    openFile();
                    break;
                case 3:
                    modifyFile();
                    break;
                case 4:
                    saveFile();
                    break;
                case 5:
                    exit();
                    break;
                default:
                    System.out.println("The command you entered is wrong( one-5)");
                    break;
            }
        }
    }

    private static void createFile() {
        message = "";
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the content and enter it when stopping\"stop\":");
        StringBuffer sBuffer = new StringBuffer();
        String inputMessage = "";
        while (!inputMessage.equals("stop")) {
            if (sBuffer.length()>0) {
                sBuffer.append("\r\n");
            }
            sBuffer.append(inputMessage);
            inputMessage = scanner.nextLine();
        }
        message = sBuffer.toString();
    }

    private static void openFile() throws Exception {
        message = "";
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the location of the open file:");
        filePath = scanner.next();
        if (filePath != null && !filePath.endsWith(".txt")) {
            System.out.println("Please select a text file!");
            return;
        }
        FileReader in = new FileReader(filePath);
        char[]charArray = new char[1024];
        int len = 0;
        StringBuffer sb = new StringBuffer();
        while((len = in.read(charArray)) != -1) {
            sb.append(charArray);
        }
        message = sb.toString();
        System.out.println("Open file contents:"+"\r\n" + message);
        in.close();
    }

    private static void modifyFile() {
        if (message == "" && filePath ==null) {
            System.out.println("Please create a new file or open it first");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the content to be modified (in Chinese)\"Modified target text: modified text\")," + "Stop modifying, please enter\"stop\":");
        String inputMessage = "";
        while(!inputMessage.equals("stop")) {
            inputMessage = scanner.nextLine();
            if (inputMessage != null && inputMessage.length() > 0) {
                String[] modifyMessage = inputMessage.split(":");
                if (modifyMessage != null && modifyMessage.length > 1) {
                    message = message.replace(modifyMessage[0], modifyMessage[1]);
                }
            }
        }
        System.out.println("Modified content:" + "\r\n" + message);
    }

    private static void saveFile() throws IOException {
        Scanner scanner = new Scanner(System.in);
        FileWriter out = null;
        if (filePath != null) {
            out = new FileWriter(filePath);
        }else {
            System.out.println("Please enter the absolute path to save the file:");
            String path = scanner.next();
            filePath = path;
            if (!filePath.toLowerCase().endsWith(".txt")) {
                filePath += ".txt";
            }
            out = new FileWriter(filePath);
        }
        out.write(message);
        out.close();
        message = "";
        filePath = null;
    }
    private static void exit() {
        System.out.println("You have exited the system, thank you for using!");
        System.exit(0);
    }

}

After running, I can see this on the console. Let me also make a simple operation.

Create a new one first.

After entering the content, press enter and enter stop to end.

Then save and try.

This error occurs because there is no java folder in my D disk and the specified directory cannot be found. It can only be saved as a txt file.
So I created a new java folder on disk D and ran without error.

Then go to disk D and look for it.
Try the open function again.

You can't enter D:\java\one here. Of course, you need a file suffix to open the file.

I also searched it myself. The Notepad written by many big guys is much more advanced than mine. I should be able to write it for java beginners.

Posted by welshy123 on Fri, 03 Sep 2021 13:30:15 -0700