Skip to main content
Code Review

Return to Question

public class FileBackup {
/**
 * Creates new file. If file with given name exists, then backups
 * it by renaming and adding time stamp to file name;
 * 
 * @param filePath the fully qualified name of file
 
 * @return the file on success; otherwise returns null
 * 
 * @throws IOException 
 */
public static File backupAndCreateFile(String filePath) throws IOException {
 File fileToCreate = new File(filePath);
 boolean backupRequired = fileToCreate.isFile();
 if (backupRequired && !backupFile(fileToCreate)) {
 return null;
 }
 File file = new File(filePath);
 return file.createNewFile() ? file : null;
}
private static final String pattern = "-dd_M_yyyy-hh_mm_ss";
private static final DateFormat dateFormat = new SimpleDateFormat(pattern); 
private static String generateBackupName (String filePath) { 
 String name = getFileName (filePath);
 String ext = getFileExtention (filePath);
 return name + dateFormat.format(new Date()) + ext;
}
private static String getFileName (String filePath) { 
 int i = filePath.lastIndexOf('.');
 return i == -1 ? filePath : filePath.substring(0, i);
}
private static String getFileExtention (String filePath) { 
 int i = filePath.lastIndexOf('.');
 return i == -1 ? "" : filePath.substring(i);
}
private static boolean backupFile (File file) {
 String filePath = file.getAbsolutePath();
 while (true) {
 String backupName = generateBackupName (filePath);
 File backup = new File(backupName); 
 if (!backup.isFile()) return file.renameTo(backup);
 // File with generated name exists, so 
 // sleep for 0.5 second and try another name
 try {
 Thread.sleep(500);
 } catch (InterruptedException e) {return false;}; // never should be here 
 }
}
}
public class FileBackup {
/**
 * Creates new file. If file with given name exists, then backups
 * it by renaming and adding time stamp to file name;
 * 
 * @param filePath the fully qualified name of file
 
 * @return the file on success; otherwise returns null
 * 
 * @throws IOException 
 */
public static File backupAndCreateFile(String filePath) throws IOException {
 File fileToCreate = new File(filePath);
 boolean backupRequired = fileToCreate.isFile();
 if (backupRequired && !backupFile(fileToCreate)) {
 return null;
 }
 File file = new File(filePath);
 return file.createNewFile() ? file : null;
}
private static final String pattern = "-dd_M_yyyy-hh_mm_ss";
private static final DateFormat dateFormat = new SimpleDateFormat(pattern); 
private static String generateBackupName (String filePath) { 
 String name = getFileName (filePath);
 String ext = getFileExtention (filePath);
 return name + dateFormat.format(new Date()) + ext;
}
private static String getFileName (String filePath) { 
 int i = filePath.lastIndexOf('.');
 return i == -1 ? filePath : filePath.substring(0, i);
}
private static String getFileExtention (String filePath) { 
 int i = filePath.lastIndexOf('.');
 return i == -1 ? "" : filePath.substring(i);
}
private static boolean backupFile (File file) {
 String filePath = file.getAbsolutePath();
 while (true) {
 String backupName = generateBackupName (filePath);
 File backup = new File(backupName); 
 if (!backup.isFile()) return file.renameTo(backup);
 // File with generated name exists, so 
 // sleep for 0.5 second and try another name
 try {
 Thread.sleep(500);
 } catch (InterruptedException e) {return false;}; // never should be here 
 }
}
}
public class FileBackup {
/**
 * Creates new file. If file with given name exists, then backups
 * it by renaming and adding time stamp to file name;
 * 
 * @param filePath the fully qualified name of file
 
 * @return the file on success; otherwise returns null
 * 
 * @throws IOException 
 */
public static File backupAndCreateFile(String filePath) throws IOException {
 File fileToCreate = new File(filePath);
 boolean backupRequired = fileToCreate.isFile();
 if (backupRequired && !backupFile(fileToCreate)) {
 return null;
 }
 File file = new File(filePath);
 return file.createNewFile() ? file : null;
}
private static final String pattern = "-dd_M_yyyy-hh_mm_ss";
private static final DateFormat dateFormat = new SimpleDateFormat(pattern); 
private static String generateBackupName (String filePath) { 
 String name = getFileName (filePath);
 String ext = getFileExtention (filePath);
 return name + dateFormat.format(new Date()) + ext;
}
private static String getFileName (String filePath) { 
 int i = filePath.lastIndexOf('.');
 return i == -1 ? filePath : filePath.substring(0, i);
}
private static String getFileExtention (String filePath) { 
 int i = filePath.lastIndexOf('.');
 return i == -1 ? "" : filePath.substring(i);
}
private static boolean backupFile (File file) {
 String filePath = file.getAbsolutePath();
 while (true) {
 String backupName = generateBackupName (filePath);
 File backup = new File(backupName); 
 if (!backup.isFile()) return file.renameTo(backup);
 // File with generated name exists, so 
 // sleep for 0.5 second and try another name
 try {
 Thread.sleep(500);
 } catch (InterruptedException e) {return false;}; // never should be here 
 }
}
}
Tidied grammar ("backup" is a noun; the verb form is "back up").
Source Link

Create and backupback up files

I have to developeddevelop a class that creates new filefiles. In case that theIf a file with the given name already exists, it should be renamed by addition of the timestamp to the file name. The file name should be human readable-readable, so I cannotcan use neither UUID nor createTempFile. The code works, but looks too bulky.

Create and backup files

I have to developed a class that creates new file. In case that the file with the given name exists, it should be renamed by addition of timestamp to the file name. The file name should be human readable, so I cannot use neither UUID nor createTempFile. The code works, but looks too bulky.

Create and back up files

I have to develop a class that creates new files. If a file with the given name already exists, it should be renamed by addition of the timestamp to the file name. The file name should be human-readable, so I can use neither UUID nor createTempFile. The code works, but looks too bulky.

Post Reopened by mdfst13, forsvarir, Phrancis, Community Bot, Mathieu Guindon
deleted 9 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

java: create Create and backup files

I have to developdeveloped a class that creates new file. In case that the file with the given name exists, then it should be renamed by addition of time stamptimestamp to the file name. The file name should be human readable, so I cannot use neither UUID nor createTempFilecreateTempFile.
Code The code works, but looks too bulky
.

java: create and backup files

I have to develop a class that creates new file. In case that the file with the given name exists, then it should be renamed by addition of time stamp to the file name. The file name should be human readable, so I cannot use neither UUID nor createTempFile.
Code works, but looks too bulky

Create and backup files

I have to developed a class that creates new file. In case that the file with the given name exists, it should be renamed by addition of timestamp to the file name. The file name should be human readable, so I cannot use neither UUID nor createTempFile. The code works, but looks too bulky.

deleted 315 characters in body; edited title
Source Link
bw_dev
  • 267
  • 2
  • 7
Loading
Post Closed as "Not suitable for this site" by Jamal
Source Link
bw_dev
  • 267
  • 2
  • 7
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /