import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.concurrent.locks.ReentrantLock;
public class FileHelper {
public static final String FILE_NAME = "currentdata.txt";
/** Общий замок для обоих потоков. */
public static final ReentrantLock LOCK = new ReentrantLock();
public static String currentTime() {
Calendar c = Calendar.getInstance();
return String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c);
}
public static String uniqueName() {
Calendar c = Calendar.getInstance();
return String.format("saved_%1$tY%1$tm%1$td_%1$tH%1$tM%1$tS_%1$tL.txt", c);
}
public static long fileSize() {
File f = new File(FILE_NAME);
return f.exists() ? f.length() : 0L;
}
public static void saveAndClear() throws IOException {
File src = new File(FILE_NAME);
if (!src.exists() || src.length() == 0) return;
File dst = new File(uniqueName());
if (!src.renameTo(dst)) {
throw new IOException("Не удалось переименовать " + src.getName());
}
try (FileOutputStream fos = new FileOutputStream(FILE_NAME, false)) {
// обнуление
}
}
}