How to copy file from one location to another location?
我想把文件从一个位置复制到Java中的另一个位置。最好的方法是什么?
以下是迄今为止我所拥有的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.List; public class TestArrayList { public static void main(String[] args) { File f = new File( "D:\\CBSE_Demo\\Demo_original\\fscommand\\contentplayer\\config"); List<String>temp=new ArrayList<String>(); temp.add(0,"N33"); temp.add(1,"N1417"); temp.add(2,"N331"); File[] matchingFiles = null; for(final String temp1: temp){ matchingFiles = f.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.startsWith(temp1); } }); System.out.println("size>>--"+matchingFiles.length); } } } |
这不会复制文件,最好的方法是什么?
您可以使用这个(或任何变体):
1 | Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING); |
另外,我建议使用
由于您不确定如何临时存储文件,请查看
1 2 |
要将文件的
1 2 3 4 5 6 7 |
使用流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | private static void copyFileUsingStream(File source, File dest) throws IOException { InputStream is = null; OutputStream os = null; try { is = new FileInputStream(source); os = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } finally { is.close(); os.close(); } } |
使用信道
1 2 3 4 5 6 7 8 9 10 11 12 | private static void copyFileUsingChannel(File source, File dest) throws IOException { FileChannel sourceChannel = null; FileChannel destChannel = null; try { sourceChannel = new FileInputStream(source).getChannel(); destChannel = new FileOutputStream(dest).getChannel(); destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); }finally{ sourceChannel.close(); destChannel.close(); } } |
使用Apache Commons IO-Lib:
1 2 3 | private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException { FileUtils.copyFile(source, dest); } |
使用Java SE 7文件类:
1 2 3 | private static void copyFileUsingJava7Files(File source, File dest) throws IOException { Files.copy(source.toPath(), dest.toPath()); } |
性能测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | File source = new File("/Users/pankaj/tmp/source.avi"); File dest = new File("/Users/pankaj/tmp/dest.avi"); //copy file conventional way using Stream long start = System.nanoTime(); copyFileUsingStream(source, dest); System.out.println("Time taken by Stream Copy ="+(System.nanoTime()-start)); //copy files using java.nio FileChannel source = new File("/Users/pankaj/tmp/sourceChannel.avi"); dest = new File("/Users/pankaj/tmp/destChannel.avi"); start = System.nanoTime(); copyFileUsingChannel(source, dest); System.out.println("Time taken by Channel Copy ="+(System.nanoTime()-start)); //copy files using apache commons io source = new File("/Users/pankaj/tmp/sourceApache.avi"); dest = new File("/Users/pankaj/tmp/destApache.avi"); start = System.nanoTime(); copyFileUsingApacheCommonsIO(source, dest); System.out.println("Time taken by Apache Commons IO Copy ="+(System.nanoTime()-start)); //using Java 7 Files class source = new File("/Users/pankaj/tmp/sourceJava7.avi"); dest = new File("/Users/pankaj/tmp/destJava7.avi"); start = System.nanoTime(); copyFileUsingJava7Files(source, dest); System.out.println("Time taken by Java7 Files Copy ="+(System.nanoTime()-start)); |
结果:
1 2 3 4 5 6 7 | /* * File copy: * Time taken by Stream Copy = 44582575000 * Time taken by Channel Copy = 104138195000 * Time taken by Apache Commons IO Copy = 108396714000 * Time taken by Java7 Files Copy = 89061578000 */ |
链接:
在Java>=7中使用新的Java文件类。
创建以下方法并导入必要的libs。
1 2 3 | public static void copyFile( File from, File to ) throws IOException { Files.copy( from.toPath(), to.toPath() ); } |
在main中使用如下创建的方法:
1 2 3 4 5 6 7 8 | File dirFrom = new File(fileFrom); File dirTo = new File(fileTo); try { copyFile(dirFrom, dirTo); } catch (IOException ex) { Logger.getLogger(TestJava8.class.getName()).log(Level.SEVERE, null, ex); } |
注意:fileFrom是要复制到其他文件夹中新文件fileTo的文件。
信用:@史葛:在Java中复制文件的标准简明方式?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public static void copyFile(File oldLocation, File newLocation) throws IOException { if ( oldLocation.exists( )) { BufferedInputStream reader = new BufferedInputStream( new FileInputStream(oldLocation) ); BufferedOutputStream writer = new BufferedOutputStream( new FileOutputStream(newLocation, false)); try { byte[] buff = new byte[8192]; int numChars; while ( (numChars = reader.read( buff, 0, buff.length ) ) != -1) { writer.write( buff, 0, numChars ); } } catch( IOException ex ) { throw new IOException("IOException when transferring" + oldLocation.getPath() +" to" + newLocation.getPath()); } finally { try { if ( reader != null ){ writer.close(); reader.close(); } } catch( IOException ex ){ Log.e(TAG,"Error closing files when transferring" + oldLocation.getPath() +" to" + newLocation.getPath() ); } } } else { throw new IOException("Old location does not exist when transferring" + oldLocation.getPath() +" to" + newLocation.getPath() ); } } |
文件(存在)
文件.创建目录()
文件,拷贝()
覆盖现有文件:文件.MOVER()
文件,删除()
文件.walkfiletree()在此处输入链接说明
将文件从一个位置复制到另一个位置意味着,需要将整个内容复制到另一个位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public static void main(String[] args) throws IOException { String destFolderPath ="D:/TestFile/abc"; String fileName ="pqr.xlsx"; String sourceFilePath="D:/TestFile/xyz.xlsx"; File f = new File(destFolderPath); if(f.mkdir()){ System.out.println("Directory created!!!!"); } else { System.out.println("Directory Exists!!!!"); } f= new File(destFolderPath,fileName); if(f.createNewFile()) { System.out.println("File Created!!!!"); } else { System.out.println("File exists!!!!"); } Files.copy(Paths.get(sourceFilePath), Paths.get(destFolderPath, fileName),REPLACE_EXISTING); System.out.println("Copy done!!!!!!!!!!!!!!"); } |