java基础案例7-3日记本

package com.itheima;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Main{
    static Scanner in = new Scanner(System.in);
    public static void main(String[] args)throws IOException{
        Enter();
    }

    //录入
    public static void Enter()throws IOException{
        String name, weather, headline, content;
        System.out.print("姓名:");
        name = in.next();
        System.out.print("天气:");
        weather = in.next();
        System.out.print("标题:");
        headline = in.next();
        System.out.print("内容:");
        content = in.next();
        Diary diary = new Diary( name, weather, headline, content);
        Record(diary);
    }

    //准备写入
    public static void Record(Diary diary)throws IOException{
        String filename = "黑马日记本.txt";
        File fileparent = new File("D:\\chapter07");
        File file = new File(fileparent,filename);
        if(file.exists())
            save(diary, file, true);
        else
            save(diary, file, false);
    }

    //写入
    public static void save(Diary diary, File file, boolean flag)throws IOException
    {
        if(flag) {
            byte[] b = ToStringBuffer(diary).getBytes();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
            bufferedOutputStream.write(b, 0, b.length);
            bufferedOutputStream.close();
        }
        else{
            byte[] b = ToStringBuffer(diary).getBytes();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            bufferedOutputStream.write(b, 0, b.length);
            bufferedOutputStream.close();
        }
    }

    //整理
    public static String ToStringBuffer(Diary diary){
        StringBuffer st = new StringBuffer();
        st.append("-----------------日记-----------------\n\n");
        st.append("                      "+diary.getHeadline()+"     \n\n");
        st.append("天气:"+diary.getWeather()+"\n");
        st.append("姓名:"+diary.getName()+"\n");
        st.append("    " + diary.getContent()+"\n\n");


        return st.toString();
    }
}




package com.itheima;

public class Diary {
    private String name;
    private String weather;
    private String headline;
    private String content;

    public Diary(String name, String weather, String headline, String content)
    {
        this.name = name;
        this.weather = weather;
        this.headline = headline;
        this.content = content;
    }
    public String getName(){
        return name;
    }
    public String getWeather(){
        return weather;
    }
    public String getHeadline(){
        return headline;
    }
    public String getContent(){
        return content;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值