English 中文(简体)
Java 利用数据和时间创建档案名称
原标题:Java, Create file name using data and time

我试图创建使用另一个类别的日期和时间的名称的档案名称,问题是这个类别经常被称作每个类别,而它是新档案。 我只是想一劳永逸地写到档案。 这样做是可能的,因为我可以不知如何工作?

许多人预先感谢任何帮助!

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("	");
        out.close();
    }catch (IOException e){
    }
}
问题回答

采用<代码>SimpleDateFormat等日期格式,以安排展期,例如,“yyyyy”制作2011年档案。 请注意,这要求退回<代码>Date。

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: 对于每小时的新档案,采用如下格式:yyyyy-MM-dd_H(例如,2011-03-29_17.tsv)。

在第1次电话中,你可以把档案名称作为该类成员储存。

页: 1

 public FileWriter( String fileName,
                    boolean append)

如果你经常写信,则另一种办法是使档案保持开放,把印刷印刷商作为成员储存。 然而,这可能会阻止在文字之间与档案进行某些外部互动。

您能否在会议上节省档案名称? 如果档案名称存在于届会中,则使用另一种明智的方法,从另一个地图集中生成。

仅凭时间,问题就在于你仍然能够同时创造多种创造力。

你可以做以下工作。

// 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+"	");

另一个问题是,你将给档案提供每份最新情况的新名称。 如果你不保留同一档案名称,那么你需要用不同的zy法,从某个领域开始计算其价值。

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.

设法添加以下方法: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("	");
    }

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签