开发者

Java, Create file name using data and time

开发者 https://www.devze.com 2023-02-21 01:59 出处:网络
I\'m trying to create a file name that uses the date and time from another class to name it, problem is the class is called every so often and when it is a new file is created. I just wanted to create

I'm trying to create a file name that uses the date and time from another class to name it, problem is the class is called every so often and when it is a new file is created. I just wanted to create the file once 开发者_如何学JAVAand then write to it all the time. Is it possible to do this as I can't work out how to?

Many thanks for any help in advance!

public void fileOutputToFile(String hex) throws Exception{

    dateAndTime dat = new dateAndTime();
    String date = dat.currentDateAndTime();

    String fileInfo = hex;
    String fileName = (date+".tsv");

    try{
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true)));
        out.print(fileInfo);
        out.print("\t");
        out.close();
    }catch (IOException e){
    }
}


Use a date formatter like SimpleDateFormat to format the date to a string, e.g. "yyyy" to make a file 2011.tsv. Note that this requires a Date object to be returned.

If you don't want to use a file based on date, store the filename somewhere. But why would you then use the date as the filename in the first place?

Edit: For a new file every hour, use a format like this: yyyy-MM-dd_HH (would result in 2011-03-29_17.tsv for example).


On the first call, you could store the filename as a member of the class.

You can append to the file on subsequent calls by using the FileWriter constructor:

 public FileWriter( String fileName,
                    boolean append)

If you write frequently, an alternative is to keep the file open, storing the PrintWriter as a member. However, this may preclude some external interactions with the file in between writes.


Can you save the file name in session? if the file name exists in session use that other wise create it from another clases..


The problem with using the time alone is you can have still have multiple creates created at the same time.

You can do the following.

// not thread safe.
private static final DateFormat DF = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS")

File file;
do {
    file = new File(DF.format(new Date())+".tsv");
} while(!file.createNewFile());
FileUtils.writeStringToFile(file, hex+"\t");

The other problem is that you will giving the file a new name on every update. If youw ant to keep the same file name you need to set this in a different method of lazy initialise the value from a field.


In order to avoid creating a new file with a new date and time every time a client application invokes method fileOutputToFile(), move all lines up to and including the creation of the PrintWriter to the constructor so that the constructor opens the file just once and method fileOutputToFile() appends fileInfo to the output stream.

Make sure to add a method close() that closes the PrintWriter stream by invoking out.close().

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MyLoggingClass {
    private PrintWriter out;

    public MyLoggingClass() throws IOException {
        dateAndTime dat = new dateAndTime();
        String date = dat.currentDateAndTime();
        String fileName = (date + ".tsv");
        out = new PrintWriter(
                new BufferedWriter(new FileWriter(fileName, true)));
    }

    public void fileOutputToFile(String hex) {
        String fileInfo = hex;
        out.print(fileInfo);
        out.print("\t");
    }

    public void close() {
        out.close();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号