Description
Copy a file from one directory to another.
License
Open Source License
Parameter
Parameter | Description |
---|
sourceDir | The directory where the file starts |
destDir | The directory where we make a copy |
filename | The file's name |
Exception
Declaration
public static void copyFile(File sourceDir, File destDir, String filename) throws java.io.IOException
Method Source Code
//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Main {
private static int BUFFERSIZE = 16 * 1024;
/**//from w w w .j a v a 2 s . c o m
Copy a file from one directory to another. <br>
This copies a file, without renaming it. The source file is not deleted.
@param sourceDir The directory where the file starts
@param destDir The directory where we make a copy
@param filename The file's name
@throws java.io.IOException on IO error
*/
public static void copyFile(File sourceDir, File destDir, String filename) throws java.io.IOException {
copyFile(sourceDir, destDir, filename, filename);
}
/**
Copy a file within a directory. <br>
This copies a file. The source file is not deleted.
@param dir The directory where the files are
@param srcFile The source file's name
@param destFile The destination file's name
@throws java.io.IOException on IO error
*/
public static void copyFile(File dir, String srcFile, String destFile) throws java.io.IOException {
copyFile(dir, dir, srcFile, destFile);
}
/**
Copy a file. <br>
This copies a file. The source file is not deleted.
@param sourceDir The directory where the file starts
@param destDir The directory where we make a copy
@param srcFile The source file's name
@param destFile The destination file's name
@throws java.io.IOException on IO error
*/
public static void copyFile(File sourceDir, File destDir, String srcFile, String destFile)
throws java.io.IOException {
File inf = new File(sourceDir, srcFile);
File outf = new File(destDir, destFile);
// open the files, copy them by buffer
FileInputStream fis = new FileInputStream(inf);
FileOutputStream fos = new FileOutputStream(outf);
copyStream(fis, fos);
fis.close();
fos.close();
return;
}
public static void copyStream(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[BUFFERSIZE];
int nRead = 0;
//int total = 0;
while (true) {
nRead = input.read(buffer, 0, BUFFERSIZE);
if (nRead < 0) {
break;
}
output.write(buffer, 0, nRead);
//total += nRead;
}
}
}
Related
- copyFile(File source, File target)
- copyFile(File source, File target)
- copyFile(File source, File target, boolean createParents, FileFilter filter)
- copyFile(File source, File target, boolean deleteSourceAfter)
- copyFile(File source, File target, boolean replaceIfExists)
- copyFile(File sourceFile, File destDir)
- copyFile(File sourceFile, File destFile)
- copyFile(File sourceFile, File destFile)
- copyFile(File sourceFile, File destFile)