import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 在一个目录下创建一个文本文件,并向文件里写入当前的创建时间,要求文件名的格式为YYYYmmDDHHMMss.txt
* @author 小苗
*
*/
public class TimeFile {
public void createFile() throws ParseException, IOException{
Date date = new Date(); //获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //定义文件内容时间格式
String formatDate = sdf.format(date); //把当前时间以定义的格式 格式化
System.out.println("文件内容时间为:"+formatDate);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss"); //定义文件名时间格式
String fileDate = sdf1.format(date); //把时间格式化成字符串
System.out.println("文件名时间为:"+fileDate);
String nameDate = "d:/"+fileDate+".txt"; //获得文件名
RandomAccessFile out_rand = new RandomAccessFile(nameDate,"rw");
out_rand.writeBytes(formatDate); //写入时间
System.out.println("创建时间文件成功,文件名为:"+nameDate);
out_rand.close();
}
public static void main(String[] args) throws ParseException, IOException {
TimeFile tf = new TimeFile();
tf.createFile();
;
}
}