1.需求html
根據原文件複製一份到指定位置java
2.代碼實現spa
須要導入:code
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;
方法封裝htm
private static final int BUFFER_SIZE = 16 * 1024; /** * <li>File文件複製本身封裝的一個把源文件對象複製成目標文件對象</li> */ private boolean copy(File src, File dst) throws Exception { dst.setWritable(true, false); boolean result = false; InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; int len = 0; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } result = true; } catch (Exception e) { throw new Exception(e); } finally { if (null != in) { try { in.close(); } catch (IOException e) { throw new Exception(e); } } if (null != out) { try { out.close(); } catch (IOException e) { throw new Exception(e); } } } return result; }