繁体   English   中英

时间戳作为Java中的文件名

[英]timestamp as file name in java

我需要在特定目录中以当前时间戳的名称创建一个文本文件

D:\\ Assignments \\ abassign在Java中。

但是当我尝试执行此操作时,出现以下错误,因为文件名不应包含“:”。 但是时间戳包含“:”

线程“主”中的异常java.io.IOException:文件名,目录名称或卷标签语法在java.io.File.createNewFile(File.java:883处的java.io.WinNTFileSystem.createFileExclusively(本机方法)处不正确)at abassign.Abassign.main(Abassign.java:35)Java结果:1

出现此错误

我用这样的东西:

    StringBuffer fn = new StringBuffer();
    fn.append(workingDirectory);
    fn.append("/");
    fn.append("fileNamePrefix-");
    fn.append("-");
    DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
    fn.append(df.format(new Date()));
    fn.append("-fileNamePostFix.txt");
    return fn.toString();

(当然,您可以删除前缀,postfix和workingDirectory部分)

如果您使用的是Windows,则文件名不能包含: ,您需要将其替换为其他字符...

保留以下字符:

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

例如 :

String yourTimeStamp = "01-01-2016 17:00:00";
File yourFile = new File("your directory", yourTimeStamp.replace(":", "_"));

这是使用时间戳生成文件的代码

/*
 * created by Sandip Bhoi 9960426568
 */
package checkapplicationisopen;


import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Random;

public class GeneratorUtil {


        /**
         * 
         * @param args 
         */
    public static void main(String args[])
    {   
            GeneratorUtil generatorUtil=new GeneratorUtil();
            generatorUtil.createNewFile("c:\\Documents", "pdf");
    }



    /**
     * 
     * @param min 
     * @param max
     * @return 
     */
    public static int randInt(int min, int max) {

        // Usually this can be a field rather than a method variable
        Random rand = new Random();

        // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }

    /**
     * 
     * @param prefix adding the prefix for time stamp generated id
     * @return the concatenated string with id and prefix
     */
    public static String getId(String prefix)
    {
        java.util.Date date = new java.util.Date();
        String timestamp = new Timestamp(date.getTime()).toString();
        String dt1 = timestamp.replace('-', '_');
        String dt2 = dt1.replace(' ', '_');
        String dt3 = dt2.replace(':', '_');
        String dt4 = dt3.replace('.', '_');
        int temp = randInt(1, 5000);
        return prefix +"_"+ temp + "_" + dt4;        
    }



        /**
         * 
         * @param direcotory
         * @param extension 
         */
        public void createNewFile(String direcotory,String extension)
        {
            try {

                File file = new File(direcotory+"//"+getId("File")+"."+extension);
                if (file.createNewFile()){
                  System.out.println("File is created!");
                }else{
                  System.out.println("File already exists.");
                }
            } catch (IOException e) {
                  e.printStackTrace();
            }
        }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM