From: <pat...@us...> - 2009-10-12 16:29:14
|
Revision: 958 http://cishell.svn.sourceforge.net/cishell/?rev=958&view=rev Author: pataphil Date: 2009-10-12 16:29:06 +0000 (Mon, 12 Oct 2009) Log Message: ----------- * Added FileUtilities.createTemporaryFileCopy, which wraps FileUtilities.copyFile. * Made custom exception type FileCopyingException for file copy operations. Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileUtilities.java Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileCopyingException.java Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileCopyingException.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileCopyingException.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileCopyingException.java 2009-10-12 16:29:06 UTC (rev 958) @@ -0,0 +1,21 @@ +package org.cishell.utilities; + +public class FileCopyingException extends Exception { + private static final long serialVersionUID = 1L; + + public FileCopyingException() { + super(); + } + + public FileCopyingException(String arg0) { + super(arg0); + } + + public FileCopyingException(Throwable arg0) { + super(arg0); + } + + public FileCopyingException(String arg0, Throwable arg1) { + super(arg0, arg1); + } +} \ No newline at end of file Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileUtilities.java 2009-10-08 21:19:28 UTC (rev 957) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileUtilities.java 2009-10-12 16:29:06 UTC (rev 958) @@ -3,10 +3,13 @@ import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.nio.channels.FileChannel; import javax.imageio.ImageIO; @@ -173,6 +176,55 @@ return readTextStringBuffer.toString(); } + public static void copyFile(File sourceFile, File targetFile) + throws FileCopyingException { + try { + FileInputStream inputStream = new FileInputStream(sourceFile); + FileOutputStream outputStream = new FileOutputStream(targetFile); + + FileChannel readableChannel = inputStream.getChannel(); + FileChannel writableChannel = outputStream.getChannel(); + + writableChannel.truncate(0); + writableChannel.transferFrom( + readableChannel, 0, readableChannel.size()); + inputStream.close(); + outputStream.close(); + } catch (IOException ioException) { + String exceptionMessage = + "An error occurred when copying from the file \"" + + sourceFile.getAbsolutePath() + + "\" to the file \"" + + targetFile.getAbsolutePath() + + "\"."; + + throw new FileCopyingException(exceptionMessage, ioException); + } + } + + public static File createTemporaryFileCopy( + File sourceFile, String fileName, String fileExtension) + throws FileCopyingException { + try { + File temporaryTargetFile = + createTemporaryFileInDefaultTemporaryDirectory( + fileName, fileExtension); + + copyFile(sourceFile, temporaryTargetFile); + + return temporaryTargetFile; + } catch (IOException temporaryFileCreationException) { + String exceptionMessage = + "An error occurred when trying to create the temporary file " + + "with file name \"" + fileName + "\" " + + "and file extension \"" + fileExtension + "\" " + + "for copying file \"" + sourceFile.getAbsolutePath() + "\"."; + + throw new FileCopyingException( + exceptionMessage, temporaryFileCreationException); + } + } + private static File ensureDirectoryExists(String directoryPath) { File directory = new File(directoryPath); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |