2013-05-15 46 views
0

我试图追加当前日期时间,文件名,并把它传递给创建文件的...所以基本上这是我的代码追加日期时间和通过它来创建文件

public class Main { 

    //.... 

    public static void main(String[] args) throws IOException 
    { 
     DateFormat dt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
     Date date = new Date(); 
     String d =dt.format(date).toString(); 
     String fname = "spy1"; 
     File dir = new File("E:\\"); 
     File f = new File(dir,fname+d+".txt"); 
     if(f.createNewFile()) 
     { 
      System.out.println("file creates"); 
     } 
     else 
     { 
      System.out.println("file not created "); 
     } 
    } 
} 

这是我的代码,请帮助我如何将当前日期时间附加到文件名并使用提到的目录创建文件

回答

5

正斜杠/和冒号:不允许在Windows上使用文件名字符。 From Naming Files, Paths, and Namespaces

 
Use any character in the current code page for a name, including Unicode 
characters and characters in the extended character set (128–255), 
except for the following: 

- The following reserved characters: 

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

- Integer value zero, sometimes referred to as the ASCII NUL character. 
- Characters whose integer representations are in the range from 1 through 31, 
except for alternate data streams where these characters are allowed. 
For more information about file streams, see File Streams. 
- Any other character that the target file system does not allow. 

日期格式字符串需要更改。例如:

DateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); 
相关问题