Hi,
I have following code which copies files from one directory to another.
private void backUpLogFile() {
final File dir = new File("." + File.separator + "log");
final
String[] subdir = dir.list();
if (!logDirectory.equals(subdir)) {
final String nameOfArchiveDir = logDirectory;
final boolean isDircreated = new File(nameOfArchiveDir).mkdir();
LOG.info("archive directory created :" + isDircreated);
}
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss", Locale.US);
FileInputStream sourceStream = null;
FileOutputStream targetStream = null;
FileChannel sourceFileChannel = null;
FileChannel targetFileChannel = null;
String logBackupFolder = "." + File.separator + logDirectory + File.separator + dateFormat.format(new Date());
final boolean newLogDir = new File(logBackupFolder).mkdir();
LOG.debug("Backup directory for log files created : " + newLogDir);
final File logDir = new File("." + File.separator + "log");
final File[] listOfFiles = logDir.listFiles();
try {
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
sourceStream = new FileInputStream("." + File.separator + "log" + File.separator + listOfFiles[i].getName());
targetStream = new FileOutputStream(logBackupFolder + File.separator + listOfFiles[i].getName());
sourceFileChannel = sourceStream.getChannel();
targetFileChannel = targetStream.getChannel();
final long size = sourceFileChannel.size();
sourceFileChannel.transferTo(0, size, targetFileChannel);
}
}
} catch (IOException e) {
LOG.error("Channel or stream is still in use, so cann't close." + e.getMessage());
}
For windows system it's fine but it's not working for unix/linux.
Can you people please tell me where is the problem.
Thank you.