Here you can find the source of copyFile(String fromFile, String toFile)
Parameter | Description |
---|---|
fromFile | the source file |
toFile | the destination file |
Parameter | Description |
---|---|
Exception | a generic exception |
public static void copyFile(String fromFile, String toFile) throws Exception
//package com.java2s; /*/*from ww w. j a v a2 s .com*/ =========================================================================== Copyright 2002-2010 Martin Dvorak Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =========================================================================== */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class Main { /** * Copy a file. * * @param fromFile * the source file * @param toFile * the destination file * @throws Exception * a generic exception */ public static void copyFile(String fromFile, String toFile) throws Exception { File from = new File(fromFile); File to = new File(toFile); to.createNewFile(); FileInputStream fileInputStream = new FileInputStream(from); BufferedInputStream in = new BufferedInputStream(fileInputStream); FileOutputStream fileOutputStream = new FileOutputStream(to); BufferedOutputStream out = new BufferedOutputStream(fileOutputStream); byte[] buffer = new byte[1024]; int i; while (in.available() > 0) { i = in.read(buffer); out.write(buffer, 0, i); } out.flush(); in.close(); out.close(); } }