Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

java - Create file name using date and time

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("");
        out.close();
    }catch (IOException e){
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...