Description
Copies a source file to a target folder
License
Open Source License
Parameter
Parameter | Description |
---|
sourceFile | the source file |
targetFolder | the target folder |
Exception
Parameter | Description |
---|
FileNotFoundException | an exception |
IOException | an exception |
Declaration
public static void copyFile(String sourceFile, String targetFolder) throws FileNotFoundException, IOException
Method Source Code
//package com.java2s;
/******************************************************************************
* Copyright (c) 2002, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors://from ww w . j a v a 2 s. c o m
* IBM Corporation - initial API and implementation
****************************************************************************/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Main {
/**
* Copies a source file to a target folder
*
* @param sourceFile
* the source file
* @param targetFolder
* the target folder
* @throws FileNotFoundException
* @throws IOException
*/
public static void copyFile(String sourceFile, String targetFolder) throws FileNotFoundException, IOException {
assert (new File(sourceFile).isFile());
assert (new File(targetFolder).isDirectory());
File source = new File(sourceFile);
copyFile(source.getParent(), targetFolder, source.getName());
}
/**
* Copies a file in a source folder to a target folder
*
* @param sourceFolder
* the source folder
* @param targetFolder
* the target folder
* @param name
* the file to copy
* @throws FileNotFoundException
* @throws IOException
*/
private static void copyFile(String sourceFolder, String targetFolder, String name)
throws FileNotFoundException, IOException {
copyFile(sourceFolder, targetFolder, name, name);
}
/**
* Copies a file in a source folder to a target folder
*
* @param sourceFolder
* the source folder
* @param targetFolder
* the target folder
* @param sourceName
* of the source file to copy
* @param targetName
* of the destination file to copy to
* @throws FileNotFoundException
* @throws IOException
*/
public static void copyFile(String sourceFolder, String targetFolder, String sourceName, String targetName)
throws FileNotFoundException, IOException {
InputStream is = new FileInputStream(sourceFolder + File.separator + sourceName);
OutputStream os = new FileOutputStream(targetFolder + File.separator + targetName);
byte[] buffer = new byte[102400];
while (true) {
int len = is.read(buffer);
if (len < 0)
break;
os.write(buffer, 0, len);
}
is.close();
os.close();
}
}
Related
- copyFile(String sourceFile, String destDir, String newFileName)
- copyFile(String sourceFile, String destFile)
- copyFile(String sourceFile, String targetFile)
- copyFile(String sourcefile, String targetfile)
- copyFile(String sourcefile, String targetFile)
- copyFile(String sourceFile, String targetFolder)
- copyFile(String sourceFilePath, String destinationFilePath)
- CopyFile(String sourceFilePath, String destinationFilePath)
- copyFile(String sourceFilePath, String destinationFilePath)