Here you can find the source of copyFile(String srcPath, String dstPath)
Parameter | Description |
---|---|
srcPath | String the source path |
dstPath | String the destination path |
public static void copyFile(String srcPath, String dstPath)
//package com.java2s; //License from project: Apache License import java.io.*; public class Main { /**//from w w w .j a v a 2s. co m * copy a file * * @param srcPath String the source path * @param dstPath String the destination path */ public static void copyFile(String srcPath, String dstPath) { InputStream inputStream = null; FileOutputStream fileOutputStream = null; try { int byteRead; File srcFile = new File(srcPath); if (srcFile.exists()) { // file exists inputStream = new FileInputStream(srcPath); // read the source file fileOutputStream = new FileOutputStream(dstPath); byte[] buffer = new byte[1444]; while ((byteRead = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, byteRead); } } } catch (Exception e) { System.out.println("copy file failed"); e.printStackTrace(); } finally { try { if (fileOutputStream != null) fileOutputStream.close(); if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }