开发中经常会遇到文件拷贝的问题,大体有两种方式,一种是Runtime.getRuntime().exec()直接用sehll命令实现拷贝,简单粗暴但是user版本貌似无效;另一种常规方式就是文件流拷贝了。 public void doCopyFile(String oldPath, String newpath) { try { String newPath = newpath; Log.d(TAG, "doCopyFile: oldPath =" + oldPath + " new path =" + newPath); int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); File newFile = new File(newPath); if(!newFile.exists()){ Log.d(TAG, "!newFile.exists()"); newFile.getParentFile().mkdirs(); } if (oldfile.exists()) { InputStream inStream = new FileInputStream(oldPath); FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1024 * 100]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { Log.d(TAG, "Exception =" + e);e.printStackTrace(); } }
Android 文件/文件目录拷贝
最新推荐文章于 2023-08-12 08:47:03 发布