Here you can find the source of copyFile(File fromFile, File toFile)
Parameter | Description |
---|---|
fromFile | a parameter |
toFile | a parameter |
public static boolean copyFile(File fromFile, File toFile)
//package com.java2s; /************************************************************** * /*from w w w . j av a2 s.c o m*/ * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * Copy a file * * @param fromFile * @param toFile * @return */ public static boolean copyFile(File fromFile, File toFile) { if (toFile.isDirectory()) toFile = new File(toFile, fromFile.getName()); FileInputStream from = null; FileOutputStream to = null; try { from = new FileInputStream(fromFile); File p = toFile.getParentFile(); if (p != null && !p.exists()) p.mkdirs(); to = new FileOutputStream(toFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead); return true; } catch (IOException e) { // Can't copy e.printStackTrace(); return false; } finally { if (from != null) try { from.close(); } catch (IOException e) { } if (to != null) try { to.close(); } catch (IOException e) { } } } /** * Copy a file * * @param fromFileName * @param toFileName * @return */ public static boolean copyFile(String fromFileName, String toFileName) { return copyFile(new File(fromFileName), new File(toFileName)); } }