2013-05-15 82 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"); 
相關問題