使用日期和时间创建文件名 [英] Create file name using date and time

查看:894
本文介绍了使用日期和时间创建文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你可以帮助我,我试图在另一个课程的日期打电话,看起来像2011-03-09 06-57-40,我想使用这个创建下面的文件,但每次当输出运行它时,它会在重新运行调用dat()时创建一个新文件。我知道发生了什么问题我只是不知道如何解决它,我想彻底地写到同一个文件。我希望这是有道理的? :/



感谢您提前提供任何帮助:)

  date d = new date(); 
String cdate = d.date();


String f = h;

try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(cdate +.tsv,true)));
out.print(f);
out.print(\t);
out.close();
} catch(IOException e){
}


解决方案

创建一个名为当前日期/时间的文件:

  Date date = new Date(); 
SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy-MM-dd HH-mm-ss);
文件文件= new File(dateFormat.format(date)+.tsv);
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(写入文件);
out.close();


I hope you could help me, I'm trying to call in the date from another class and looks like "2011-03-09 06-57-40", I want to use this to create the file below but everytime I do when the output runs it creates a new file as it re-runs calling the dat(). I know what's going wrong I'm just not sure how to fix it, I want to permently writw to the same file. I hope this makes sense? :/

Thank you for any help in advance :)

    date d = new date();
    String  cdate = d.date();


    String f = h;

    try{
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(cdate + ".tsv", true)));
        out.print(f);
        out.print("\t");
        out.close();
    }catch (IOException e){
    }

解决方案

To create a file named the current date/time:

Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;
File file = new File(dateFormat.format(date) + ".tsv") ;
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("Writing to file");
out.close();

这篇关于使用日期和时间创建文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆