public static void main(String[] args) {
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date2=sdf2.format(date);
String fileName=sdf.format(date);
String path="E:\\log\\"+fileName+".txt";
File file=new File(path);
if(!file.exists()){
try {
FileOutputStream out=new FileOutputStream(file,true);
out.write("hao".getBytes());
out.write(",".getBytes());
out.write("192.168.1.106,".getBytes());
out.write(date2.getBytes());
out.write("\n".getBytes());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
try {
RandomAccessFile raf=new RandomAccessFile(path,"rw");
raf.seek(raf.length());
raf.write("lin,192.168.1.200,".getBytes());
raf.write(date2.getBytes());
raf.write("\n".getBytes());
raf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上代码是每天根据年月日生产一个记事本文件,用年月日来做为记事本的名字。如果根据当天的年月日命名的记事本不存在,那么就自动创建记事本。否则的话。就向该记事本中写入你想写入的信息。