2016-08-02 8 views
-3

i für eine neue Datei existiert in Ordner Code versuche oder nicht durch spezifische Zeitstempel durch javacode vorbei, aber ich bin nicht immer es ..can u helfen ...Datei existiert oder nicht vergehenden Zeit

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

Ich habe keine Ahnung, was Sie beschreiben möchten. Außerdem, was "Ich verstehe es nicht" bedeutet. Was ist das eigentliche Problem? – David

Antwort

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."); 
    } 

} 
Verwandte Themen