Java基础案例教程 第七章IO(输入输出)———【任务7-2】模拟记事本 ()

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

一、任务描述:

二、运行结果:

 

三、实现思路:

1、不缺定输入次数,用

 while (true) {
            System.out.print("请输入操作指令:");
            Scanner sc = new Scanner(System.in);
            int command = sc.nextInt();

2、新建文件

读取键盘输入是以行尾单位,最后的文本内容需要拼接,因此用

StringBuffer str = new StringBuffer();
str.append(line).append("\r\n");   //记录当前行内容

Scanner交替接收Int  和  String  

 sc.nextLine();  //吸收上次nextInt() 剩下的回车符
 public static void createFile() {
        System.out.println("请输入内容,停止填写请输入“stop”");
        sc.nextLine();  //吸收上次nextInt() 剩下的回车符
        //Scanner sc = new Scanner(System.in);
        StringBuffer str = new StringBuffer();
        String line = null;
        while (true) {

            line = sc.nextLine();
            if (line.equals("stop")) {   //不能用equals!!!
                break;
            }
            str.append(line).append("\r\n");   //记录当前行内容
        }
        //记录所有内容到message
        message = str.toString();
    }

3、打开文件

文件流读取只能 读单个字符或者是字符数组,也需要 StringBuffer 拼接

public static void openFile() throws IOException {
        System.out.print("请输入打开文件位置: ");
        //Scanner sc = new Scanner(System.in);
        sc.nextLine();  //吸收上次nextInt() 剩下的回车符
        path = sc.nextLine();
        //判断文件路径是否正确
        if (path.endsWith(".txt")) {
            StringBuffer sb = new StringBuffer();
            FileReader in = new FileReader(path);

            //文件流读取只能 读单个字符或者是字符数组
            char[] chs = new char[1024];
            int len = 0;
            //把字符读到数组里 len:长度
            while ((len = in.read(chs)) != -1) {
                sb.append(chs, 0, len); //采用StringBuffer存储文件中所有的字符
            }
            in.close();
            //把文件内容记录在message中
            message = sb.toString();
            System.out.println("打开文本内容:\r\n" + message);
        } else {
            System.out.println("请选择文本文件");
        }
    }

4、修改文件

 public static void editFile() {
        System.out.println("请输入要修改的内容,以“修改的目标文字:修改之后的文字”格式,");
        System.out.println("修改结束请输入“stop”");
        sc.nextLine();  //吸收上次nextInt() 剩下的回车符
        //Scanner sc = new Scanner(System.in);
        String line = "";
        //可能输入好几行
        while (true) {
            line=sc.next();
            //line = sc.nextLine();
            if (line.equals("stop")) {
                break;
            }
            String[] edit = line.split(":");
            message = message.replace(edit[0], edit[1]);
        }
        System.out.println("修改后的文本内容是\r\n" + message);
    }

5、保存

1)字符流追加式写出

FileWriter out=new FileWriter(path,true);

2)普通覆盖式写出

 FileWriter out = new FileWriter(path); 
 public static void saveFile() throws IOException {
            System.out.print("请输入文件保存的绝对路径:");      
            //Scanner sc = new Scanner(System.in);
            sc.nextLine();  //吸收上次nextInt() 剩下的回车符
            path = sc.nextLine();
            if (path != "") {
                /*
                 * 追加式写出  FileWriter out=new FileWriter(path,true);
                 */
                FileWriter out = new FileWriter(path);   //覆盖式写入

                //写入文件,可以直接用String
                out.write(message);
                out.close();
            }
    }

6、退出

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

四、实现代码:


package cn.itcast.task02;

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

/**
 * @author wangyue
 * @version 1.0
 * @date 2019/7/3 13:33
 * @describe: 模拟记事本
 * <p>
 * 3、Scanner会有缓存
 */
public class Notapad {
    public static Scanner sc = new Scanner(System.in);
    public static String message = null; //静态方法可以访问静态变量和其他静态方法,但不能访问实例变量和实例方法
    public static String path = null;

    public static void main(String[] args) throws IOException {
        System.out.println("---1:新建文件, 2:打开文件, 3:修改文件, 4:保存, 5:退出---");

        while (true) {
            System.out.print("请输入操作指令:");
            //Scanner sc = new Scanner(System.in);
            int command = sc.nextInt();
            switch (command) {
                case 1: //1:新建文件
                    createFile();
                    break;
                case 2: //2:打开文件
                    openFile();
                    break;
                case 3: //3:修改文件
                    editFile();
                    break;
                case 4: //4:保存
                    saveFile();
                    break;
                case 5: //5:退出
                    exit();
                    break;
                default:
                    System.out.println("您输入的参数不合法:");
                    break;
            }
        }

    }


    public static void createFile() {
        System.out.println("请输入内容,停止填写请输入“stop”");
        sc.nextLine();  //吸收上次nextInt() 剩下的回车符
        //Scanner sc = new Scanner(System.in);
        StringBuffer str = new StringBuffer();
        String line = null;
        while (true) {

            line = sc.nextLine();
            if (line.equals("stop")) {   //不能用equals!!!
                break;
            }
            str.append(line).append("\r\n");   //记录当前行内容
        }
        //记录所有内容到message
        message = str.toString();
    }

    public static void openFile() throws IOException {
        System.out.print("请输入打开文件位置: ");
        //Scanner sc = new Scanner(System.in);
        sc.nextLine();  //吸收上次nextInt() 剩下的回车符
        path = sc.nextLine();
        //判断文件路径是否正确
        if (path.endsWith(".txt")) {
            StringBuffer sb = new StringBuffer();
            FileReader in = new FileReader(path);

            //文件流读取只能 读单个字符或者是字符数组
            char[] chs = new char[1024];
            int len = 0;
            //把字符读到数组里 len:长度
            while ((len = in.read(chs)) != -1) {
                sb.append(chs, 0, len); //采用StringBuffer存储文件中所有的字符
            }
            in.close();
            //把文件内容记录在message中
            message = sb.toString();
            System.out.println("打开文本内容:\r\n" + message);
        } else {
            System.out.println("请选择文本文件");
        }
    }

    public static void editFile() {
        System.out.println("请输入要修改的内容,以“修改的目标文字:修改之后的文字”格式,");
        System.out.println("修改结束请输入“stop”");
        sc.nextLine();  //吸收上次nextInt() 剩下的回车符
        //Scanner sc = new Scanner(System.in);
        String line = "";
        //可能输入好几行
        while (true) {
            line=sc.next();
            //line = sc.nextLine();
            if (line.equals("stop")) {
                break;
            }
            String[] edit = line.split(":");
            message = message.replace(edit[0], edit[1]);
        }
        System.out.println("修改后的文本内容是\r\n" + message);
    }


    public static void saveFile() throws IOException {
            System.out.print("请输入文件保存的绝对路径:");
            //Scanner sc = new Scanner(System.in);
            sc.nextLine();  //吸收上次nextInt() 剩下的回车符
            path = sc.nextLine();
            if (path != "") {
                /*
                 * 追加式写出  FileWriter out=new FileWriter(path,true);
                 */
                FileWriter out = new FileWriter(path);   //覆盖式写入

                //写入文件,可以直接用String
                out.write(message);
                out.close();
            }
    }


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


}


猜你喜欢

转载自blog.csdn.net/wangpailiulanqi8/article/details/94553794