控制台留言板,主要实现文件操作的功能

文件操作的第一段代码,Mark一下!

package eveDay.hw151223;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;

public class LyMan {
    // 添加留言的功能,只是不断往后面添加
    public void addLy(String allPath) {
        Scanner input = new Scanner(System.in);
        Date date = new Date(System.currentTimeMillis());
        File file = new File(allPath);
        try {
            FileWriter fw = new FileWriter(file, true);
            BufferedWriter bw = new BufferedWriter(fw);
            System.out.println("请输入留言标题:");
            String topic = input.nextLine();
            System.out.println("请输入留言内容:");
            String content = input.nextLine();
            String time = (1900 + date.getYear()) + "-" + (date.getMonth() + 1)
                    + "-" + date.getDate() + " " + date.getHours() + ":"
                    + date.getMinutes() + ":" + date.getSeconds();
            System.out.println("当前时间:" + time + "\n留言信息已经保存");
            bw.write(topic + "|" + time + "|" + content);
            bw.newLine();
            bw.close();
            fw.close();
        } catch (IOException e) {
            System.out.println("文件不可写,改为可写!");
            file.setWritable(true);
            System.out.println("已改为可写!");
        }
    }

    // 显示全部留言
    public void showLy(String pathname) {
        File file = new File(pathname);
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            if (line == null) {
                System.out.println("没有留言信息!");
            }
            int count = 1;
            while (line != null) {
                String[] lines = line.split("\\|");
                System.out.println("序号:" + count + "\t标题:" + lines[0] + "\t时间:"
                        + lines[1] + "\n内容:" + lines[2]
                        + "\n-------------------------------------------\n\n");
                count++;
                line = br.readLine();
            }
            br.close();
            fr.close();
        } catch (FileNotFoundException e) {
            System.out.println("没有文件,新建留言板文件!");
            try {
                file.createNewFile();
            } catch (IOException e1) {
                System.out.println("文件不可写,改为可写!");
                file.setWritable(true);
                System.out.println("已改为可写!");
            }
        } catch (IOException e) {
            System.out.println("文件不可写,改为可写!");
            file.setWritable(true);
            System.out.println("已改为可写!");
        }

    }

    public static void main(String[] args) {
        String prjPath = System.getProperty("user.dir");
        String fileName = "messageboard.txt";
        LyMan lyman = new LyMan();

        System.out.println("欢迎来到本站点进行留言");
        exit: while (true) {
            System.out.println("1.发布留言\t2.查询留言\t3.退出系统");
            Scanner input = new Scanner(System.in);
            int choice = 0;
            try {
                choice = input.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入错误请重新输入:");
                choice = 0;
            }
            switch (choice) {
            case 1:
                lyman.addLy(prjPath + "\\" + fileName);
                break;
            case 2:
                lyman.showLy(prjPath + "\\" + fileName);
                break;
            case 3:
                System.out.println("退出系统");
                break exit;
            default:
                System.out.println("功能项选择错误,请重新输入!");
                break;
            }
        }
    }
}

可能会出现的错误:
1.输入时,添加“|”会出现结果错误。解决方法,把输入部分,单独做一个方法,在方法内解决这些问题。

    private String inputString() {
        Scanner input = new Scanner(System.in);
        String inputs = "";
        while (true) {
            inputs = input.nextLine();
            if (inputs.contains("|")) {
                System.out.println("含有非法字符“|”,请重新输入!!!");
            } else {
                break;
            }
        }
        return inputs;
    }

2。当保存的留言数据比较多时,Console里面可能会出现显示错误的问题。解决方案:更改Console的设置,Fixed width console->Maximum character width:80或者其他数值。取消勾选Limit console output..
目前就只找到这些BUG。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值