Java basic writing notepad

Learned some basics of Java, and wrote a functional super simple notepad.

First of all, a large framework needs to be established, such as Windows Notepad, which has the functions of creating new files, opening files, modifying files, saving files, etc. Without further ado, just upload the code directly.

    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:新建文件 2:打开文件 3:修改文件 4:保存 5:退出");
        //create,open,modify,save,exit
        while(true) {
    
    
            System.out.println("请输入指令:");
            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("您输入的指令错误!(1-5)");
                    break;
            }
        }
    }

I don't know how to describe some specific points so that they can be easily understood. Anyway, after learning it, I know how to write it. I haven't searched the Internet for how other people write it. This should be the simplest.
Next, there are a few small frames, so I won’t say much. After running all the codes, the operation is very simple, and you should be able to get started directly after reading it.
create a new file

private static void createFile() {
    
    
        message = "";
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容,停止时输入\"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 a file

private static void openFile() throws Exception {
    
    
        message = "";
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入打开文件的位置:");
        filePath = scanner.next();
        if (filePath != null && !filePath.endsWith(".txt")) {
    
    
            System.out.println("请选择文本文件!");
            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("打开文件内容:"+"\r\n" + message);
        in.close();
    }

modify file

private static void modifyFile() {
    
    
        if (message == "" && filePath ==null) {
    
    
            System.out.println("请先新建文件或者打开文件");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的内容(以\"修改的目标文字:修改之后的文字\")," + "停止修改请输入\"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("修改后的内容:" + "\r\n" + message);
    }

save document

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("请输入文件保存的绝对路径:");
            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;
    }

quit

private static void exit() {
    
    
        System.out.println("您已退出系统,谢谢使用!");
        System.exit(0);
    }

Some methods used in the code definitely need to import the package, and the complete code follows.

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:新建文件 2:打开文件 3:修改文件 4:保存 5:退出");
        while(true) {
    
    
            System.out.println("请输入指令:");
            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("您输入的指令错误!(1-5)");
                    break;
            }
        }
    }

    private static void createFile() {
    
    
        message = "";
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容,停止时输入\"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("请输入打开文件的位置:");
        filePath = scanner.next();
        if (filePath != null && !filePath.endsWith(".txt")) {
    
    
            System.out.println("请选择文本文件!");
            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("打开文件内容:"+"\r\n" + message);
        in.close();
    }

    private static void modifyFile() {
    
    
        if (message == "" && filePath ==null) {
    
    
            System.out.println("请先新建文件或者打开文件");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的内容(以\"修改的目标文字:修改之后的文字\")," + "停止修改请输入\"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("修改后的内容:" + "\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("请输入文件保存的绝对路径:");
            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("您已退出系统,谢谢使用!");
        System.exit(0);
    }

}

This is what you see in the console after running, so let me do a simple operation.
insert image description here
Create a new one first.
insert image description here
After entering the content, press Enter and enter stop to end.
insert image description here
Then save it and try it out.
insert image description here
This error occurs because there is no java folder in my D drive, and the specified directory cannot be found. It can only be saved directly as a txt file.
So I created a new java folder on the D drive and ran it without error.

insert image description here
Then go to the D drive to look for it. insert image description here
Try to open the function again.
insert image description here
You cannot enter D:\java\one here, and of course you need a file suffix to open the file.

This is the end of the code. If you find it helpful, don’t forget to give it a thumbs up.

Guess you like

Origin blog.csdn.net/m0_53692627/article/details/120090810