From e6517134e3663460509a53a254dd61a664e08dcd Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 11 Nov 2013 18:28:18 +0200 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A03.=20=D0=97=D0=B0=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tse/lr3/DirCopy.java | 90 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/src/tse/lr3/DirCopy.java b/src/tse/lr3/DirCopy.java index e1eb2f6..1e197be 100644 --- a/src/tse/lr3/DirCopy.java +++ b/src/tse/lr3/DirCopy.java @@ -1,25 +1,111 @@ package tse.lr3; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileFilter; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.channels.FileChannel; /** * * @author aNNiMON */ public class DirCopy extends AbstractDirectoryChooser { + + private static final int DAYS = 3; public static void main() { new DirCopy().setVisible(true); } - private File dirSource; + private File dirSource, dirDest, logFile; + private BufferedWriter writer; public DirCopy() { super("Копирование директорий"); + createFilesAndDirs(); + } + + private void createFilesAndDirs() { + dirDest = new File("E:\\LR_3\\task3"); + dirDest.mkdirs(); + logFile = new File("E:\\LR_3\\task3_log.txt"); + try { + writer = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(logFile, true), "UTF-8") + ); + } catch (IOException ex) { + ex.printStackTrace(); + } } @Override protected void directorySelected(File directory) { - System.out.println(directory.getAbsolutePath()); + dirSource = directory; + + dirDest.mkdirs(); + File[] files = dirSource.listFiles(lastModifiedFilter); + if (files.length > 0) { + writeLog("Копируем из " + dirSource.getAbsolutePath() + " в " + dirDest.getAbsolutePath()); + for (File file : files) { + try { + copyFile(file, dirDest); + writeLog(file.getAbsolutePath()); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } else writeLog("Нечего копировать из " + dirSource.getAbsolutePath()); + + if (writer != null) { + try { + writer.flush(); + writer.close(); + } catch (IOException ex) {} + } } + + private void copyFile(File sourceFile, File destDirectory) throws IOException { + File destFile = new File(destDirectory, sourceFile.getName()); + if (!destFile.exists()) { + destFile.createNewFile(); + } + + FileChannel source = null; + FileChannel destination = null; + try { + source = new FileInputStream(sourceFile).getChannel(); + destination = new FileOutputStream(destFile).getChannel(); + destination.transferFrom(source, 0, source.size()); + } finally { + if (source != null) { + source.close(); + } + if (destination != null) { + destination.close(); + } + } + } + + private void writeLog(String filename) { + System.out.println(filename); + try { + writer.write(filename); + writer.newLine(); + } catch (IOException ex) { } + } + + private final FileFilter lastModifiedFilter = new FileFilter() { + + @Override + public boolean accept(File pathname) { + if (pathname.isDirectory()) return false; + + long lastDays = System.currentTimeMillis() - (DAYS * 24 * 60 * 60 * 1000); + return (pathname.lastModified() >= lastDays); + } + }; } \ No newline at end of file