Description
Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
License
Apache License
Parameter
Parameter | Description |
---|
in | Source data stream |
destFile | Destination file |
Exception
Parameter | Description |
---|
IOException | an exception |
Declaration
public static void copyFile(InputStream in, String destFile) throws IOException
Method Source Code
//package com.java2s;
/**/*from www . ja v a 2s . c om*/
* Copyright (C) 2012 Red Hat, Inc. and/or its affiliates.
*
* 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.*;
public class Main {
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
/**
* Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
*
* @param in Source data stream
* @param destFile Destination file
* @throws IOException
*/
public static void copyFile(InputStream in, String destFile) throws IOException {
copyFile(in, destFile, DEFAULT_BUFFER_SIZE);
}
/**
* Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
*
* @param sourceFile Source file
* @param destFile Destination file
* @throws IOException
*/
public static void copyFile(String sourceFile, String destFile) throws IOException {
copyFile(sourceFile, destFile, DEFAULT_BUFFER_SIZE);
}
/**
* Copy a file to another file using a buffer of maximum size
*
* @param sourceFile Source file
* @param destFile Destination file
* @param copyBufferSize Maximum size of the buffer
* @throws IOException
*/
public static void copyFile(String sourceFile, String destFile, int copyBufferSize) throws IOException {
// Abro los ficheros
FileInputStream in = new FileInputStream(sourceFile);
copyFile(in, destFile, copyBufferSize);
}
/**
* Copy a file to another file using a buffer of maximum size
*
* @param in Source data stream
* @param destFile Destination file
* @param copyBufferSize Maximum size of the buffer
* @throws IOException
*/
public static void copyFile(InputStream in, String destFile, int copyBufferSize) throws IOException {
// Abro los ficheros
FileOutputStream out = new FileOutputStream(destFile);
try {
// Transpaso de informacion
byte[] data = new byte[copyBufferSize];
int readSize;
while ((readSize = in.read(data)) != -1)
out.write(data, 0, readSize);
} finally {
// Cierro los ficheros
in.close();
out.close();
}
}
}
Related
- copyFile(final String sourceFile, final String destinationFile)
- copyFile(final String sourceFilePath, final String destFilePath)
- copyFile(final String sSource, final String sDest)
- copyFile(InputStream in, File dst)
- copyFile(InputStream in, File to)
- copyFile(OutputStream out, InputStream in)
- copyFile(String f1, String f2)
- copyFile(String fileIn, String fileOut)
- copyFile(String fileInName, String fileOutName)