From: Eric P. <th...@us...> - 2010-10-30 17:33:35
|
Update of /cvsroot/sandev/sand/apps/basics/src/org/sandev/basics/util In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv13012 Modified Files: FileUtil.java Log Message: Added niocopyfile utility for channel based file copying. Index: FileUtil.java =================================================================== RCS file: /cvsroot/sandev/sand/apps/basics/src/org/sandev/basics/util/FileUtil.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileUtil.java 24 Jan 2010 21:01:37 -0000 1.2 --- FileUtil.java 30 Oct 2010 17:33:27 -0000 1.3 *************** *** 29,32 **** --- 29,33 ---- import java.io.BufferedOutputStream; import java.io.FileOutputStream; + import java.nio.channels.FileChannel; *************** *** 53,67 **** /** ! * Copy the specified file srcfile to outfile */ public static void copyfile(File srcfile,File outfile) throws IOException { ! OutputStream fileOut=null; ! InputStream fileIn=null; ! try { if(!srcfile.exists()) { ! throw new IOException( ! "copyfile srcfile " + srcfile + " not found."); } fileIn=new BufferedInputStream(new FileInputStream(srcfile)); fileOut=new BufferedOutputStream(new FileOutputStream(outfile)); --- 54,78 ---- /** ! * Copy srcfile to outfile. ! */ ! public static void copyfile(String srcfile,String outfile) ! throws IOException ! { ! copyfile(new File(srcfile),new File(outfile)); ! } ! ! ! /** ! * Copy the specified srcfile to outfile using buffered streams */ public static void copyfile(File srcfile,File outfile) throws IOException { ! OutputStream fileOut=null; ! InputStream fileIn=null; ! try { if(!srcfile.exists()) { ! throw new IOException("copyfile srcfile " + srcfile + ! " does not exist."); } fileIn=new BufferedInputStream(new FileInputStream(srcfile)); fileOut=new BufferedOutputStream(new FileOutputStream(outfile)); *************** *** 70,78 **** while((bytesread=fileIn.read(buf))>0) { fileOut.write(buf,0,bytesread); } ! } finally { if(fileOut!=null) { fileOut.close(); } if(fileIn!=null) { fileIn.close(); } } } --- 81,116 ---- while((bytesread=fileIn.read(buf))>0) { fileOut.write(buf,0,bytesread); } ! } finally { if(fileOut!=null) { fileOut.close(); } if(fileIn!=null) { fileIn.close(); } + } + } + + + /** + * Copy the specified srcfile to outfile using FileChannel direct + * transfer. + */ + public static void niocopyfile(File srcfile,File outfile) + throws IOException + { + if(!srcfile.exists()) { + throw new IOException("copyfile srcfile " + srcfile + + " does not exist."); } + if(!outfile.exists()) { + outfile.createNewFile(); } + FileChannel fcSrc=null; + FileChannel fcOut=null; + try { + fcSrc=new FileInputStream(srcfile).getChannel(); + fcOut=new FileOutputStream(outfile).getChannel(); + fcOut.transferFrom(fcSrc,0,fcSrc.size()); + } finally { + if(fcSrc!=null) { + fcSrc.close(); } + if(fcOut!=null) { + fcOut.close(); } } } |