Here you can find the source of copyFile(File source, File destination)
Parameter | Description |
---|---|
source | a parameter |
destination | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFile(File source, File destination) throws IOException
//package com.java2s; /*/*from w w w . ja v a 2s .c o m*/ * Copyright 2011 cruxframework.org. * * Licensed 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.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { /** * Copies a file; * @param source * @param destination * @throws IOException */ public static void copyFile(File source, File destination) throws IOException { FileOutputStream out = null; FileInputStream in = null; try { destination.getParentFile().mkdirs(); out = new FileOutputStream(destination); in = new FileInputStream(source); byte[] buff = new byte[1024]; int read = 0; while ((read = in.read(buff)) > 0) { out.write(buff, 0, read); } out.flush(); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } /** * * @param file * @return * @throws IOException */ public static String read(File file) throws IOException { return read(new FileInputStream(file)); } /** * @param in * @return * @throws IOException */ public static String read(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int read = 0; byte[] buff = new byte[1024]; while ((read = in.read(buff)) > 0) { out.write(buff, 0, read); } in.close(); out.flush(); out.close(); return new String(out.toByteArray()); } /** * @param text * @param f * @throws IOException */ public static void write(String text, File f) throws IOException { FileOutputStream out = new FileOutputStream(f); out.write(text.getBytes()); out.close(); } /** * @param in * @param f * @throws IOException */ public static void write(InputStream in, File f) throws IOException { FileOutputStream out = new FileOutputStream(f); int read = 0; byte[] buff = new byte[1024]; while ((read = in.read(buff)) > 0) { out.write(buff, 0, read); } in.close(); out.close(); } }