使用JAVA基础以及IO流做出的一个简易博客系统

实现的需求:
1.安装功能(在磁盘上创建总目录)
2.卸载功能(删除总目录以及其所有子项)
3.可跨时间、跨设备的登录、注册、修改密码功能(用户资料存在磁盘中,可随程序带走)
4.可跨时间、跨设备的写博客、改博客、删博客、查看博客功能(博客存在相应的用户文件夹中)
5.所有操作在Eclipse控制台实现

使用的技术:
1.java语法基础
2.Date、SimpleDateFormat、Calendar三个类(用于博客自动生成最后修改时间)
3.部分I/O流

第一步,建立用户类,用于存放和创建用户信息

public class UserInfo implements Serializable{
    /**
     * 版本号:1
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private String pwd;
    private String phone;
    public UserInfo() {
        super();

    }
    public UserInfo(String name, String pwd, String phone) {
        super();
        this.name = name;
        this.pwd = pwd;
        this.phone = phone;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

}

第二步,建立博客类,用于存放和创建博客内容

public class Blogs extends UserInfo implements Serializable{
    /**
     * 版本号:2
     */
    private static final long serialVersionUID = 2L;
    private String title;
    private String content;
    private String time;
    public Blogs() {
        super();

    }
    public Blogs(String title, String content, String time) {
        super();
        this.title = title;
        this.content = content;
        this.time = time;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    //重写toString方法,以备可能需要的输出
    @Override
    public String toString() {
        return "标题:" + title + "\n内容:" + content + "\n最后修改时间:" + time;
    }

}

第三步,创建功能接口,其实这一步也可以省去,如果不用这一步,只需要把第四步的代码中的@Override全部删去,不再实现此接口即可

public interface Functions {
boolean set();//安装功能
boolean unSet();//卸载功能
boolean addUser(String name,String pwd,String phone) throws Exception;//注册
boolean login(String name,String pwd) throws Exception;//登录
boolean modUser(String name,String phone,String newPwd) throws Exception;//修改密码
boolean addBlog(String title,String content) throws Exception;//写博客
boolean modBlog(String title,String content) throws Exception;//修改博客
boolean deleteBlog(String title);//删除博客
void look() throws Exception;//遍历博客
}

第四步,创建实现类,将所有的功能重写

public class FunctionImpl implements Functions{//功能实现类
    Scanner scan = new Scanner(System.in);
    private String name;
    @Override
    public boolean set() {//安装功能的方法
        File file = new File("Blog");
        if(file.exists()){
            System.out.println("检测到程序已安装,请不要重复安装哦");
        }else{
            file.mkdir();
            return true;
        }
        return false;
    }

    @Override
    public boolean unSet() {//卸载功能的方法
        File file = new File("Blog");
        if(!file.exists()){
            System.out.println("还没有安装,怎么卸载呢");
        }else{
            delDir(file);
            file.delete();
            return true;
        }
        return false;
    }

    @Override
    public boolean addUser(String name,String pwd,String phone) throws Exception {//用户注册的方法
        File file = new File("Blog" + File.separator + name);
        if(file.exists()){
            System.out.println("用户名已存在");
        }else{
            file.mkdir();
            cretUser(name,pwd,phone);
            return true;
        }
        return false;
    }

    @Override
    public boolean login(String name, String pwd) throws Exception {//登录的方法
        UserInfo user = getUser(name);
        if(pwd.equals(user.getPwd())){
            this.name = user.getName();
            return true;
        }
        return false;
    }

    @Override
    public boolean modUser(String name,String phone,String newPwd) throws Exception {//修改密码的方法
        UserInfo user = getUser(name);
        if(phone.equals(user.getPhone())){
            user.setPwd(newPwd);
            FileOutputStream fos = new FileOutputStream("Blog" + File.separator + name +File.separator + name + ".user");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(user);
            oos.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean addBlog(String title, String content) throws Exception {//写博客的方法
        File file = new File("Blog" + File.separator + this.name +File.separator + title + ".blog");
        if(file.exists()){
            System.out.println("标题已存在,请选择修改或换个标题");
        }else{

            Calendar calendar = Calendar.getInstance();
            Date date = calendar.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
            String time = sdf.format(date);
            cretBlog(title,content,time);
            return true;
        }
        return false;
    }

    @Override
    public boolean modBlog(String title,String content) throws Exception {//修改博客的方法
        Blogs blog = getBlog(title);
        if(blog.getTitle()!=null){
            Calendar calendar = Calendar.getInstance();
            Date date = calendar.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
            String time = sdf.format(date);
            blog.setContent(content);
            blog.setTime(time);
            FileOutputStream fos = new FileOutputStream("Blog" + File.separator + this.name +File.separator + title + ".blog");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(blog);
            oos.close();
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deleteBlog(String title) {//删除博客的方法
        File file = new File("Blog" + File.separator + this.name + File.separator + title + ".blog");
        if(file.exists()){
            file.delete();
            return true;
        }else{
            System.out.println("该博客不存在");
            return false;
        }
    }

    @Override
    public void look() throws Exception {//遍历博客的方法
        File file = new File("Blog" + File.separator + this.name);
        File[] files = file.listFiles();
        for(File f : files){
            if(f.getName().endsWith(".blog")){
                FileInputStream fis = new FileInputStream(f);
                ObjectInputStream ois = new ObjectInputStream(fis);
                Object o = ois.readObject();
                if(o instanceof Blogs){
                    Blogs blog = (Blogs)o;
                    System.out.println(blog);
                }
                ois.close();
            }
        }

    }
    public void delDir(File file){//删除多级目录的方法
        File[] files = file.listFiles();
        for(File f : files){
            if(f.isDirectory()){
                delDir(f);
            }
            f.delete();
        }
    }

    public void cretBlog(String title,String content,String time) throws Exception{//创建博客文件的方法
        Blogs blog = new Blogs(title,content,time);
        FileOutputStream fos = new FileOutputStream("Blog" + File.separator + this.name +File.separator + title + ".blog");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(blog);
        oos.close();
    }
    public void cretUser(String name,String pwd,String phone) throws Exception{//创建用户信息文件的方法
        UserInfo user = new UserInfo(name,pwd,phone);
        FileOutputStream fos = new FileOutputStream("Blog" + File.separator + name +File.separator + name + ".user");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(user);
        oos.close();
    }

    public Blogs getBlog(String title) throws Exception{//根据标题获取博客内容的方法
        File file = new File("Blog" + File.separator + this.name +File.separator + title + ".blog");
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        if(!file.exists()){
            System.out.println("该博客不存在!");
        }else{
            Object o = ois.readObject();
            if(o instanceof Blogs){
                Blogs blog = (Blogs)o;
                ois.close();
                return blog;
        }
        }
        ois.close();
        return new Blogs();
    }
    public UserInfo getUser(String name) throws Exception{//根据用户名获取用户信息的方法
        File file = new File("Blog" + File.separator + name);
        FileInputStream fis = new FileInputStream("Blog" + File.separator + name +File.separator + name +".user");
        ObjectInputStream ois = new ObjectInputStream(fis);
        if(!file.exists()){
            System.out.println("该用户名不存在!");
        }else{
            Object o = ois.readObject();
            if(o instanceof UserInfo){
                UserInfo user = (UserInfo)o;
                ois.close();
                return user;
            }
        }
        ois.close();
        return new UserInfo();
    }

    public boolean cheak(){//检测功能是否安装的方法
        File file = new File("Blog");
        if(file.exists()){
            return true;
        }
        return false;
    }
    public void systemList() throws Exception{//系统菜单

        System.out.println("————————————系统菜单————————————");
        System.out.println("1.安装功能\t2.卸载功能\t3.登录菜单\t0.退出");
        String p = scan.next();
        switch(p){
        case "1":
            if(set()){
                System.out.println("安装成功!");
            }else{
                System.out.println("安装失败!");
            }
            systemList();
            break;
        case "2":
            if(unSet()){
                System.out.println("卸载成功,有缘再见");
                System.exit(0);
            }else{
                System.out.println("卸载失败!");
            }
            systemList();
            break;
        case "3":
            if(cheak()){
                loginList();
                return;
            }else{
                System.out.println("你还没有安装功能呢!");
            }
            systemList();
            break;
        case "0":
            System.out.println("欢迎下次使用");
            System.exit(0);
            break;
        default:
            System.out.println("请输入正确的指令");
            systemList();
            break;
        }
    }

    public void loginList() throws Exception{//登录菜单
        System.out.println("————————————登录菜单————————————");
        System.out.println("1.登录\t2.注册\t3.忘记密码\t0.返回上一级");
        String p = scan.next();
        switch(p){
        case "1":
            System.out.println("登录用户名:");
            String name = scan.next();
            System.out.println("登录密码:");
            String pwd = scan.next();
            if(login(name,pwd)){
                System.out.println("登录成功!");
                blogsList();
                return;
            }else{
                System.out.println("登录失败,请检查用户名和密码是否正确!");
            }
            loginList();
            break;
        case "2":
            System.out.println("注册用户名:");
            String name1 = scan.next();
            System.out.println("注册密码:");
            String pwd1 = scan.next();
            System.out.println("注册手机号(找回密码时重要凭证):");
            String phone = scan.next();

            if(addUser(name1,pwd1,phone)){
                System.out.println("注册成功!");
            }else{
                System.out.println("注册失败!");
            }
            loginList();
            break;
        case "3":
            System.out.println("————找回密码————");
            System.out.println("需要找回密码的用户名:");
            String name2 = scan.next();
            System.out.println("绑定的手机号:");
            String phone1 = scan.next();
            System.out.println("新密码:");
            String pwd2 = scan.next();
            if(modUser(name2,phone1,pwd2)){
                System.out.println("修改密码成功!");
            }else{
                System.out.println("修改密码失败!");
            }
            loginList();
            break;
        case "0":
            systemList();
            break;
        default:
            System.out.println("请输入正确的指令");
            loginList();
            break;
        }
    }
    public void blogsList() throws Exception{//博客菜单
        System.out.println("————————————博客菜单————————————");
        System.out.println("1.写博客\t2.改博客\t3.删博客\t4.查看博客\t0.返回登录界面");
        String p = scan.next();
        switch(p){
        case "1":
            System.out.println("新博客标题:");
            String title = scan.next();
            System.out.println("新博客内容:");
            String content = scan.next();
            if(addBlog(title,content)){
                System.out.println("新博客保存成功!");
            }else{
                System.out.println("操作失败!");
            }
            blogsList();
            break;
        case "2":
            System.out.println("要修改的博客标题:");
            String title1 = scan.next();
            System.out.println("新的博客内容");
            String content1 = scan.next();

            if(modBlog(title1,content1)){
                System.out.println("修改成功!");
            }else{
                System.out.println("修改失败!");
            }
            blogsList();
            break;
        case "3":
            System.out.println("要删除的博客标题:");
            String title2 = scan.next();
            if(deleteBlog(title2)){
                System.out.println("删除成功!");
            }else{
                System.out.println("删除失败!");
            }
            blogsList();
            break;
        case "4":
            look();
            blogsList();
            break;
        case "0":
            loginList();
            break;
        default:
            System.out.println("请输入正确的指令");
            blogsList();
            break;
        }
    }
}

第五步,程序测试

public class BlogSystem {
    public static void main(String[] args) throws Exception {
        System.out.println("————————————木荣叶华博客系统————————————");
        FunctionImpl impl = new FunctionImpl();
        impl.systemList();
    }
}

注意:以上所有文件与目录均在此工程的根目录下创建,更改路径时最好自己知道存在哪里,实在找不到又不想留下垃圾时使用“卸载功能”即可

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值