public static void copyFile(String from_name, String to_name) throws IOException { File from_file = new File(from_name); // Get File objects from Strings File to_file = new File(to_name); String parent = to_file.getParent(); // The destination directory if (parent == null) // If none, use the current directory parent = System.getProperty("user.dir"); File dir = new File(parent); // Convert it to a file. dir.mkdir(); FileInputStream from = null; // Stream to read from source FileOutputStream to = null; // Stream to write to destination try { from = new FileInputStream(from_file); // Create input stream to = new FileOutputStream(to_file); // Create output stream byte[] buffer = new byte[4096]; // To hold file contents int bytes_read; // How many bytes in buffer while((bytes_read = from.read(buffer)) != -1) // Read until EOF to.write(buffer, 0, bytes_read); // write }
throws IOException
{
File from_file = new File(from_name); // Get File objects from Strings
File to_file = new File(to_name); String parent = to_file.getParent(); // The destination directory
if (parent == null) // If none, use the current directory
parent = System.getProperty("user.dir");
File dir = new File(parent); // Convert it to a file.
dir.mkdir(); FileInputStream from = null; // Stream to read from source
FileOutputStream to = null; // Stream to write to destination
try {
from = new FileInputStream(from_file); // Create input stream
to = new FileOutputStream(to_file); // Create output stream
byte[] buffer = new byte[4096]; // To hold file contents
int bytes_read; // How many bytes in buffer while((bytes_read = from.read(buffer)) != -1) // Read until EOF
to.write(buffer, 0, bytes_read); // write
}
finally {
if (from != null) try { from.close(); } catch (IOException e) { ; }
if (to != null) try { to.close(); } catch (IOException e) { ; }
}
}