檔案的複製和剪下功能,簡單實現,複製剪下功能實現
個人寫的檔案的複製方法和剪下方法,是一個初出茅廬的小猿猿,有什麼缺點,請各位大神指出
/** * * @param tarPath 把檔案複製到那個路徑 * @param sourcePath 複製檔案的路徑 * @throws IOException */ // 檔案的複製功能 public void copyFile(String tarPath, String sourcePath) throws IOException { File tarFile = new File(tarPath); if(!tarFile.exists()){ tarFile.createNewFile(); } File sourceFile = new File(sourcePath); try { // 建立一個檔案輸入資料流 FileInputStream fis = new FileInputStream(sourceFile); // 輸入資料流緩衝區 BufferedInputStream bis = new BufferedInputStream(fis); byte[] buffer = new byte[bis.available()]; bis.read(buffer); bis.close(); fis.close(); // 建立一個檔案輸出資料流 FileOutputStream fos = new FileOutputStream(tarFile); BufferedOutputStream bos = new BufferedOutputStream(fos); // 將資料寫入輸出資料流 bos.write(buffer); // 此處需要重新整理 bos.flush(); bos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //刪除檔案(也可以當做複製完檔案以後 ,剪下功能) /** * * @param sourcePath 要刪除的檔案路徑 */ public void deleteFile(String sourcePath){ File tarFile = new File(sourcePath); if(tarFile.isFile()) tarFile.delete(); }