Top Description Fields Constructors Methods
org.springframework.util

public abstract Class FileCopyUtils

extends Object
Class Inheritance
Imports
java.io.ByteArrayInputStream, .Closeable, .File, .IOException, .InputStream, .OutputStream, .Reader, .StringWriter, .Writer, java.nio.file.Files, org.springframework.lang.Nullable

Simple utility methods for file and stream copying. All copy methods use a block size of 4096 bytes, and close all affected streams when done. A variation of the copy methods from this class that leave streams open can be found in StreamUtils.

Mainly for use within the framework, but also useful for application code.

Authors
Juergen Hoeller, Hyunjin Choi
Since
06.10.2003
See Also
StreamUtils, FileSystemUtils

Field Summary

Modifier and TypeField and Description
public static final int
BUFFER_SIZE

The default buffer size used when copying bytes.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
close(Closeable
the Closeable to close
closeable
)

Attempt to close the supplied Closeable, silently swallowing any exceptions.

public static int

Returns:

the number of bytes copied
copy
(File
the file to copy from
in
,
File
the file to copy to
out
)

Copy the contents of the given input File to the given output File.

public static void
copy(byte[]
the byte array to copy from
in
,
File
the file to copy to
out
)

Copy the contents of the given byte array to the given output File.

public static int

Returns:

the number of bytes copied
copy
(InputStream
the stream to copy from
in
,
OutputStream
the stream to copy to
out
)

Copy the contents of the given InputStream to the given OutputStream.

public static void
copy(byte[]
the byte array to copy from
in
,
OutputStream
the OutputStream to copy to
out
)

Copy the contents of the given byte array to the given OutputStream.

public static int

Returns:

the number of characters copied
copy
(Reader
the Reader to copy from
in
,
Writer
the Writer to copy to
out
)

Copy the contents of the given Reader to the given Writer.

public static void
copy(String
the String to copy from
in
,
Writer
the Writer to copy to
out
)

Copy the contents of the given String to the given Writer.

public static byte[]

Returns:

the new byte array that has been copied to
copyToByteArray
(File
the file to copy from
in
)

Copy the contents of the given input File into a new byte array.

public static byte[]

Returns:

the new byte array that has been copied to (possibly empty)
copyToByteArray
(InputStream
the stream to copy from (may be null or empty)
in
)

Copy the contents of the given InputStream into a new byte array.

public static String

Returns:

the String that has been copied to (possibly empty)
copyToString
(Reader
the reader to copy from (may be null or empty)
in
)

Copy the contents of the given Reader into a String.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUFFER_SIZEback to summary
public static final int BUFFER_SIZE

The default buffer size used when copying bytes.

Constructor Detail

FileCopyUtilsback to summary
public FileCopyUtils()

Method Detail

closeback to summary
private static void close(Closeable closeable)

Attempt to close the supplied Closeable, silently swallowing any exceptions.

Parameters
closeable:Closeable

the Closeable to close

copyback to summary
public static int copy(File in, File out) throws IOException

Copy the contents of the given input File to the given output File.

Parameters
in:File

the file to copy from

out:File

the file to copy to

Returns:int

the number of bytes copied

Exceptions
IOException:
in case of I/O errors
copyback to summary
public static void copy(byte[] in, File out) throws IOException

Copy the contents of the given byte array to the given output File.

Parameters
in:byte[]

the byte array to copy from

out:File

the file to copy to

Exceptions
IOException:
in case of I/O errors
copyback to summary
public static int copy(InputStream in, OutputStream out) throws IOException

Copy the contents of the given InputStream to the given OutputStream. Closes both streams when done.

Parameters
in:InputStream

the stream to copy from

out:OutputStream

the stream to copy to

Returns:int

the number of bytes copied

Exceptions
IOException:
in case of I/O errors
copyback to summary
public static void copy(byte[] in, OutputStream out) throws IOException

Copy the contents of the given byte array to the given OutputStream. Closes the stream when done.

Parameters
in:byte[]

the byte array to copy from

out:OutputStream

the OutputStream to copy to

Exceptions
IOException:
in case of I/O errors
copyback to summary
public static int copy(Reader in, Writer out) throws IOException

Copy the contents of the given Reader to the given Writer. Closes both when done.

Parameters
in:Reader

the Reader to copy from

out:Writer

the Writer to copy to

Returns:int

the number of characters copied

Exceptions
IOException:
in case of I/O errors
copyback to summary
public static void copy(String in, Writer out) throws IOException

Copy the contents of the given String to the given Writer. Closes the writer when done.

Parameters
in:String

the String to copy from

out:Writer

the Writer to copy to

Exceptions
IOException:
in case of I/O errors
copyToByteArrayback to summary
public static byte[] copyToByteArray(File in) throws IOException

Copy the contents of the given input File into a new byte array.

Parameters
in:File

the file to copy from

Returns:byte[]

the new byte array that has been copied to

Exceptions
IOException:
in case of I/O errors
copyToByteArrayback to summary
public static byte[] copyToByteArray(InputStream in) throws IOException

Copy the contents of the given InputStream into a new byte array. Closes the stream when done.

Parameters
in:InputStream

the stream to copy from (may be null or empty)

Returns:byte[]

the new byte array that has been copied to (possibly empty)

Exceptions
IOException:
in case of I/O errors
copyToStringback to summary
public static String copyToString(Reader in) throws IOException

Copy the contents of the given Reader into a String. Closes the reader when done.

Parameters
in:Reader

the reader to copy from (may be null or empty)

Returns:String

the String that has been copied to (possibly empty)

Exceptions
IOException:
in case of I/O errors