开发者

Appending a unique identifier to a file

I'm Rakesh I'm developing an application that needs to archive files with same names again and ag开发者_JS百科ain. The files thus zipped are stored in a folder. Now let me get into the point I want to append the time stamp as a unique identifier n that's where the problem arises. When I give any normal int value or anything of that sort it is working fine. It throws an exception only in case of date format. I'm appending the code for u to go through. Kindly go through the code and let me know where I'm going wrong.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Files 
{
public static void main(String args[])throws Exception
{   
    byte[] buf = new byte[1024];
    int len;
    int i=1;
    DateFormat df = new SimpleDateFormat("hh:mm:ss");  
    df.setTimeZone(TimeZone.getTimeZone("PST"));  
    String t=df.format(new Date());
    FileInputStream fis=new FileInputStream("e:/Xxx.java");
    File f=File.createTempFile("aaaa"+t, ".zip", new File("D:/"));
    FileOutputStream fos=new FileOutputStream(f);
    ZipOutputStream zip=new ZipOutputStream(fos);
    zip.putNextEntry(new ZipEntry( "D:/aaa"+t+".zip"));
     while ((len = fis.read(buf)) > 0) 
     {
            zip.write(buf, 0, len);

     }
    System.out.println("done");
}

}

Exception message:

Exception in thread "main" java.io.IOException: The parameter is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.checkAndCreate(File.java:1704)
at java.io.File.createTempFile(File.java:1792)
at Files.main(Files.java:21)


According to this page, ':' cannot be used in a Windows file name. Change the ':'s to '-'s (or get rid of them entirely) and you should be fine.


You will need to replace the colons in the formatted date to legal characters, such as dashes ("-").


DateFormat df = new SimpleDateFormat("hh-mm-ss");

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜