如何以编程方式将文件复制到另一个目录?(How to copy programmatically a file to another directory?) |
您所在的位置:网站首页 › 怎么复制一个文件的路径图片 › 如何以编程方式将文件复制到另一个目录?(How to copy programmatically a file to another directory?) |
如何以编程方式将文件复制到另一个目录?(How to copy programmatically a file to another directory?)
目录中有一个image file 。 如何copy这个image file copy到刚刚创建的另一个directory中? 这两个directories位于设备的同一个internal storage上:) There is an image file inside a directory. How to copy this image file into another directory that was just created ? The two directories are on the same internal storage of the device :) 最满意答案您可以使用这些功能。 如果你传入一个文件,第一个将复制整个目录与所有的孩子或一个文件。 第二个只对文件有用,并且在第一个文件中被调用。 另请注意,您需要具有执行此操作的权限 功能: public static void copyFileOrDirectory(String srcDir, String dstDir) { try { File src = new File(srcDir); File dst = new File(dstDir, src.getName()); if (src.isDirectory()) { String files[] = src.list(); int filesLength = files.length; for (int i = 0; i < filesLength; i++) { String src1 = (new File(src, files[i]).getPath()); String dst1 = dst.getPath(); copyFileOrDirectory(src1, dst1); } } else { copyFile(src, dst); } } catch (Exception e) { e.printStackTrace(); } } public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.getParentFile().exists()) destFile.getParentFile().mkdirs(); 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(); } } }You can use these functions. The first one will copy whole directory with all children or a single file if you pass in a file. The second one is only usefull for files and is called for each file in the first one. Also note you need to have permissions to do that Functions: public static void copyFileOrDirectory(String srcDir, String dstDir) { try { File src = new File(srcDir); File dst = new File(dstDir, src.getName()); if (src.isDirectory()) { String files[] = src.list(); int filesLength = files.length; for (int i = 0; i < filesLength; i++) { String src1 = (new File(src, files[i]).getPath()); String dst1 = dst.getPath(); copyFileOrDirectory(src1, dst1); } } else { copyFile(src, dst); } } catch (Exception e) { e.printStackTrace(); } } public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.getParentFile().exists()) destFile.getParentFile().mkdirs(); 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(); } } }更多推荐 image,file,directory,directories,internal,电脑培训,计算机培训,IT培训"/> |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |