java中實現檔案複製的方法
阿新 • • 發佈:2019-01-30
路徑: String oldpath = "/sdcard/youku/HyteraAppStore.apk"; String newpath = "/sdcard/youku/youku_vip_logger/HyteraAppStore.apk";
複製方法:
public void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { //檔案存在時 InputStream inStream = new FileInputStream(oldPath); //讀入原檔案 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //位元組數 檔案大小 System.out.println(bytesum); Log.d("pppp","bytesum====== " + bytesum); fs.write(buffer, 0, byteread); } inStream.close(); Log.d("pppp","======success====="); } } catch (Exception e) { System.out.println("複製單個檔案操作出錯"); e.printStackTrace(); } }
另附:字串擷取方法(根據需要取字串中“,”(逗號)後面的字元)
reqResult = "hello,world"; String getSignInfo = reqResult.substring(reqResult.indexOf(",") + 1);