DiaryOrder.java
package diary;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class DiaryOrder {
static ArrayList<Diary> diaryList=new ArrayList<Diary>();
public static void main(String[] args) throws Exception{
System.out.println("---------日记本案例---------");
boolean flag=true;
Scanner scan=new Scanner(System.in);
while(flag){
System.out.println("1.编写日记 2.查看日记");
System.out.println("请选择编号");
int a = scan.nextInt();
if(a==1){
System.out.println("请输入姓名:");
String name=scan.next();
System.out.println("请输入天气:");
String weather=scan.next();
System.out.println("请输入标题:");
String title=scan.next();
System.out.println("请输入内容:");
String content=scan.next();
Diary diary=addDiary(name,weather,title,content);
FileUtil.savediarys(diary);
}
else if(a==2){
FileUtil.readFile();
}
else{
System.out.println("输入错误");
flag=false;
}
}
}
private static Diary addDiary(String name,String weather,String title,String content){
Date date = new Date();
DateFormat dformat = new SimpleDateFormat("yyyy-MM-dd");
String dates=dformat.format(date).toString();
Diary diary=new Diary(dates,name,weather,title,content);
return diary;
}
}
FileUtil.java
package diary;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class FileUtil {
public static final String SEPARATE_FITELD =" ";
public static final String SEPARATE_LINE ="\r\n";
public static void savediarys(Diary diary) {
String name ="日记本.txt";
InputStream in = null;
try{
in = new FileInputStream(name);
if(in!=null){
in.close();
creatFile(name,true,diary);}
}
catch (FileNotFoundException e){
creatFile(name,false,diary);
}
catch (IOException e){
e.printStackTrace();
}
}
public static void creatFile(String name,boolean label,Diary diary){
BufferedOutputStream out = null;
StringBuffer sbf=new StringBuffer();
try{
if(label){
out=new BufferedOutputStream (new FileOutputStream(name,true));
}
else{
out=new BufferedOutputStream (new FileOutputStream(name));
String fieldSort = "欢迎来到日记本";
sbf.append(fieldSort).append(SEPARATE_LINE);
}
sbf.append("时间:").append(diary.time).append(SEPARATE_FITELD);
sbf.append("姓名:").append(diary.name).append(SEPARATE_LINE);
sbf.append("标题:").append(diary.title).append(SEPARATE_FITELD);
sbf.append("天气:").append(diary.weather).append(SEPARATE_LINE);
sbf.append("内容:").append(diary.content).append(SEPARATE_LINE);
String str=sbf.toString();
byte[] b=str.getBytes();
for(int i=0;i<b.length;i++){
out.write(b[i]);
}
}catch (Exception e){
e.printStackTrace();
}finally{
try{
if(out !=null)
out.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
}
public static void readFile() throws Exception{
FileInputStream in = new FileInputStream("日记本.txt");
byte[] b = new byte[in.available()];
in.read(b);
String str = new String(b);
System.out.println(str);
in.close();
}
}
Diary.java
package diary;
public class Diary {
String time;
String name;
String weather;
String title;
String content;
public Diary(String time, String name, String weather, String title,
String content) {
super();
this.time = time;
this.name = name;
this.weather = weather;
this.title = title;
this.content = content;
}
}