Here you can find the source of copyFile(File source, File destination)
Parameter | Description |
---|---|
source | a parameter |
destination | a parameter |
public static boolean copyFile(File source, File destination)
//package com.java2s; /******************************************************************************* * Copyright (c) 2004 Ferenc Hechler - ferenc_hechler@users.sourceforge.net * //from w w w .j av a2s . co m * This file is part of the Fat Jar Eclipse Plug-In * * The Fat Jar Eclipse Plug-In is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License as published by the Free Software Foundation; * either version 2 of the License, or (at your option) any later version. * * The Fat Jar Eclipse Plug-In is distributed * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the Fat Jar Eclipse Plug-In; * if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *******************************************************************************/ import java.io.ByteArrayInputStream; 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 { /** * copy source to destination, missing dirs are created, * existing destination-file will be overwritten * @param source * @param destination * @return true on success */ public static boolean copyFile(File source, File destination) { boolean ok = false; InputStream in = null; try { mkDirs(destination.getParentFile()); in = new FileInputStream(source); writeToFile(destination, in); in = null; } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } return ok; } /** * create all missing folders and return true, * if folder on success * @param f - folder to create */ public static boolean mkDirs(File f) { boolean ok = false; if (f.isDirectory()) ok = true; else if (f.exists()) ok = false; else { ok = f.mkdirs(); } return ok; } /** * writes a stream into a file. Existing files will be overwritten . * @param outputFile - target file to be created * @param in - stream to be written to file, Stream will be closed. * @return <code>true</code> on success */ public static boolean writeToFile(File outputFile, InputStream in) { return writeToFile(outputFile, in, true); } /** * writes a stream into a file. Existing files will be overwritten . * @param outputFile - target file to be created * @param in - stream to be written to file * @param closeIn if true, in-Stream will be closed * @return <code>true</code> on success */ public static boolean writeToFile(File outputFile, InputStream in, boolean closeIn) { boolean ok = false; OutputStream out = null; try { out = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int cnt = 0; if (in != null) cnt = in.read(buffer); while (cnt > 0) { out.write(buffer, 0, cnt); cnt = in.read(buffer); } ok = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (closeIn && (in != null)) try { in.close(); } catch (IOException e) { } if (out != null) try { out.close(); } catch (IOException e) { } } return ok; } /** * @param conflictOutputFile * @param newManifest */ public static void writeToFile(File outputFile, String content) { InputStream stream = new ByteArrayInputStream(content.getBytes()); writeToFile(outputFile, stream); } }