
Paste_Image.png

Paste_Image.png
/*
* 我的日记本:
* 创建日记本文件 ————》特定的命名格式
* File createFile(); 返回创建好的日及文件,将最后的文件内容保存在该文件内。
*
* 创建一个面板,可以设置背景及文字。具有富文本格式。
* 可以运用 swing 组件, 及jTextArea对象 对富文本的支持。
*
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.metal.OceanTheme;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class MyDiary {
private Calendar c=Calendar.getInstance();
String name;
public MyDiary(){
}
public MyDiary(String name){
this.name=name;
}
public String getName(){
return name;
}
@SuppressWarnings("resource")
public void writeFile() {
String name_JFrame="day"+c.get(Calendar.DAY_OF_MONTH)+"-"+name;
JFrame frame=new JFrame(name_JFrame);
//OceanTheme theme=new OceanTheme();
JTextPane text=new JTextPane();
StyledDocument doc=text.getStyledDocument();
SimpleAttributeSet wordStyle=new SimpleAttributeSet();
text.setBackground(Color.lightGray);
StyleConstants.setForeground(wordStyle,Color.BLACK);
StyleConstants.setFontSize(wordStyle, 20);
StyleConstants.setFontFamily(wordStyle, "宋体");
//StyleConstants.setBackground(wordStyle, Color.GREEN);
text.setEditable(true);
text.setText(".LOG\r\n");
doc.setCharacterAttributes(24, 30, wordStyle, true);
frame.add(new JScrollPane(text),BorderLayout.CENTER);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize.width/2, (int) (screenSize.height/1.5));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
BufferedWriter br=null;
Scanner sca=null;
try{
br=new BufferedWriter(new FileWriter(createFile(),true));
sca=new Scanner(new File("my.stx"));
while(sca.hasNextLine()){
br.write(sca.nextLine());
}
/*try{
br.write(doc.getText(0,doc.getLength()));
}
catch(BadLocationException ex){
throw new RuntimeException("存储失败"+ex.getMessage());
}*/
//if(!frame.isValid())
br.flush();
}
catch(IOException e){
throw new RuntimeException("写入失败"+e.getMessage());
}
finally{
try{
if(br!=null)
br.close();
}
catch(IOException e){
throw new RuntimeException("操作流失败"+e.getMessage());
}
}
}
public String createFile(){
String s1="E:/无知/每日一文/";//绝对路径
String s2=month()+c.get(Calendar.YEAR);//文件夹
String s3="/day"+c.get(Calendar.DAY_OF_MONTH)+"-"+name;//文件名
String s4=".txt";//文件后缀名
String path=s1+s2+s3+s4;
String dir=s1+s2;
File f=null;
BufferedWriter bw=null;
try{
f=new File(dir);
if(!f.exists()) f.mkdirs();
f=new File(path);
f.createNewFile();
bw=new BufferedWriter(new FileWriter(f,true));
bw.write(".LOG");
bw.newLine();
bw.close();
return path;
}
catch(IOException e){
System.out.println("创建文件失败");
System.out.print(e.toString());
return path;
}
finally{
try{
if(bw!=null)
bw.close();
}
catch(IOException e){
System.out.println("发生错误");
}
}
}
public String month(){
switch(c.get(Calendar.MONTH)){
case 0:return "January";
case 1:return "February";
case 2:return "March";
case 3:return "April";
case 4:return "May";
case 5:return "June";
case 6:return "July";
case 7:return "August";
case 8:return "September";
case 9:return "October";
case 10:return "November";
case 11:return "December";
default :return "0";
}
}
}
网友评论