2016-08-02 39 views
-3

我試圖代碼,一個新的文件夾中或者不通過傳遞特定的時間戳通過javacode存在,但我沒有得到它..canü幫助...文件存在或不經過時間

String filePath = System.getProperty("user.dir"); 
    //create a new file with Time Stamp 
    File file = new File(filePath + "\\" + filename+GetCurrentTimeStamp().replace(":","_").replace(".","_")+".txt"); 

    try { 
     if (!file.exists()) { 
      file.createNewFile(); 
      System.out.println("File is created; file name is " + file.getName()); 
     } else { 
      System.out.println("File already exist"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
    // Get current system time 
public static String GetCurrentTimeStamp() { 
    SimpleDateFormat sdfDate = new SimpleDateFormat(
      "yyyy-MM-dd HH:mm:ss.SSS");// dd/MM/yyyy 
    Date now = new Date(); 
    String strDate = sdfDate.format(now); 
    return strDate; 
} 
// Get Current Host Name 
public static String GetCurrentTestHostName() throws UnknownHostException { 
    InetAddress localMachine = InetAddress.getLocalHost(); 
    String hostName = localMachine.getHostName(); 
    return hostName; 
} 

// Get Current User Name 
public static String GetCurrentTestUserName() { 
    return System.getProperty("user.name"); 
+2

我不知道你在描述什麼。另外,詳細說明「我沒有得到它」的含義。什麼是實際問題? – David

回答

0
package com.test; 

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class trst { 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     boolean b = false; 
     File file, file1; 
     Date today = new Date(); 
       SimpleDateFormat ft = new SimpleDateFormat ("MMddyyyyhhmmss"); 
       String folder_name="backup_"+ft.format(today); 
      file = new File("./"+folder_name); 
     if (!file.exists()) 
     { 
      b = file.mkdirs(); 
     } 
     file1 = new File(file+"/"+"Test"); 
     FileWriter fileWriter = new FileWriter(file1); 
     fileWriter.write("abc"); 
     fileWriter.flush(); 
     fileWriter.close(); 
     if (b) 
     System.out.println("Backup has been created."); 
     else 
     System.out.println("Failed to create backup."); 
    } 

}