Class IO
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic File
Convert an object to aFile
if possible.static void
Closes an arbitrary closable, and logs exceptions at ignore levelstatic void
close
(InputStream is) closes an input stream, and logs exceptionsstatic void
close
(OutputStream os) closes an output stream, and logs exceptionsstatic void
closes a reader, and logs exceptionsstatic void
closes a writer, and logs exceptionsstatic void
close
(AutoCloseable closeable) Closes an arbitrary closable, and logs exceptions at ignore levelstatic void
Copy files or directoriesstatic void
copy
(InputStream in, OutputStream out) Copy Stream in to Stream out until EOF or exception.static void
copy
(InputStream in, OutputStream out, long byteCount) Copy Stream in to Stream for byteCount bytes or until EOF or exception.static void
Copy Reader to Writer out until EOF or exception.static void
Copy Reader to Writer for byteCount bytes or until EOF or exception.static void
static void
Copy the contents of a source directory to destination directory.static void
copyDir
(Path srcDir, Path destDir, CopyOption... copyOptions) Deprecated, for removal: This API element is subject to removal in a future version.static void
Copy the contents of a source file to destination file.static void
Copy the contents of a source file to destination file.static boolean
Delete File.static boolean
Delete the path, recursively.static void
ensureDirExists
(Path dir) Ensure that the given path exists, and is a directory.static boolean
isEmptyDir
(File dir) Test if directory is empty.static byte[]
readBytes
(InputStream in) static Path
resolvePath
(Path basePath, Path relative) Perform a resolve of abasePath
Path
against arelative
Path
in a way that ignoresFileSystem
differences between the twoPath
parameters.static IOException
static String
toString
(InputStream in) Read input stream to string.static String
toString
(InputStream in, String encoding) Read input stream to string.static String
toString
(InputStream in, Charset encoding) Read input stream to string.static String
Read input stream to string.static String
Read Path to string.static long
write
(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length) A gathering write utility wrapper.
-
Field Details
-
CRLF
- See Also:
-
CRLF_BYTES
public static final byte[] CRLF_BYTES -
bufferSize
public static final int bufferSize- See Also:
-
-
Method Details
-
copy
Copy Stream in to Stream out until EOF or exception.- Parameters:
in
- the input stream to read from (until EOF)out
- the output stream to write to- Throws:
IOException
- if unable to copy streams
-
copy
Copy Reader to Writer out until EOF or exception.- Parameters:
in
- the read to read from (until EOF)out
- the writer to write to- Throws:
IOException
- if unable to copy the streams
-
copy
Copy Stream in to Stream for byteCount bytes or until EOF or exception.- Parameters:
in
- the stream to read fromout
- the stream to write tobyteCount
- the number of bytes to copy- Throws:
IOException
- if unable to copy the streams
-
copy
Copy Reader to Writer for byteCount bytes or until EOF or exception.- Parameters:
in
- the Reader to read fromout
- the Writer to write tobyteCount
- the number of bytes to copy- Throws:
IOException
- if unable to copy streams
-
copy
Copy files or directories- Parameters:
from
- the file to copyto
- the destination to copy to- Throws:
IOException
- if unable to copy
-
copyDir
- Throws:
IOException
-
copyDir
Copy the contents of a source directory to destination directory.This version does not use the standard
Files.copy(Path, Path, CopyOption...)
technique to copy files, as that technique might incur a "foreign target" behavior when theFileSystem
types of the srcDir and destDir are different. Instead, this implementation uses thecopyFile(Path, Path)
method instead.- Parameters:
srcDir
- the source directorydestDir
- the destination directory- Throws:
IOException
- if unable to copy the file
-
copyDir
@Deprecated(since="12.0.8", forRemoval=true) public static void copyDir(Path srcDir, Path destDir, CopyOption... copyOptions) throws IOException Deprecated, for removal: This API element is subject to removal in a future version.usecopyDir(Path, Path)
instead to avoid foreign target behavior across FileSystems.Copy the contents of a source directory to destination directory.Copy the contents of srcDir to the destDir using
Files.copy(Path, Path, CopyOption...)
to copy individual files.- Parameters:
srcDir
- the source directorydestDir
- the destination directory (must exist)copyOptions
- the options to use on theFiles.copy(Path, Path, CopyOption...)
commands.- Throws:
IOException
- if unable to copy the file
-
resolvePath
Perform a resolve of abasePath
Path
against arelative
Path
in a way that ignoresFileSystem
differences between the twoPath
parameters.This implementation is intended to be a replacement for
Path.resolve(Path)
in cases where the theFileSystem
might be different, avoiding aProviderMismatchException
from occurring.- Parameters:
basePath
- the base Pathrelative
- the relative Path to resolve against base Path- Returns:
- the new Path object relative to the base Path
-
ensureDirExists
Ensure that the given path exists, and is a directory.Uses
Files.createDirectories(Path, FileAttribute[])
when the provided path needs to be created as directories.- Parameters:
dir
- the directory to check and/or create.- Throws:
IOException
- if thedir
exists, but isn't a directory, or if unable to create the directory.
-
copyFile
Copy the contents of a source file to destination file.Copy the contents of
srcFile
to thedestFile
usingFiles.copy(Path, OutputStream)
. ThedestFile
is opened with theOpenOption
ofStandardOpenOption.CREATE
,StandardOpenOption.WRITE
,StandardOpenOption.TRUNCATE_EXISTING
.Unlike
Files.copy(Path, Path, CopyOption...)
, this implementation will not perform a "foreign target" behavior (a special mode that kicks in when thesrcFile
anddestFile
are on differentFileSystem
s) which will attempt to delete the destination file before creating a new file and then copying the contents over.In this implementation if the file exists, it will just be opened and written to from the start of the file.
- Parameters:
srcFile
- the source file (must exist)destFile
- the destination file- Throws:
IOException
- if unable to copy the file
-
copyFile
Copy the contents of a source file to destination file.Copy the contents of
from
File
to theto
File
using standardInputStream
/OutputStream
behaviors.- Parameters:
from
- the source file (must exist)to
- the destination file- Throws:
IOException
- if unable to copy the file
-
rethrow
-
toString
Read Path to string.- Parameters:
path
- the path to read from (until EOF)charset
- the charset to read with- Returns:
- the String parsed from path (default Charset)
- Throws:
IOException
- if unable to read the path (or handle the charset)
-
toString
Read input stream to string.- Parameters:
in
- the stream to read from (until EOF)- Returns:
- the String parsed from stream (default Charset)
- Throws:
IOException
- if unable to read the stream (or handle the charset)
-
toString
Read input stream to string.- Parameters:
in
- the stream to read from (until EOF)encoding
- the encoding to use (can be null to use default Charset)- Returns:
- the String parsed from the stream
- Throws:
IOException
- if unable to read the stream (or handle the charset)
-
toString
Read input stream to string.- Parameters:
in
- the stream to read from (until EOF)encoding
- the Charset to use (can be null to use default Charset)- Returns:
- the String parsed from the stream
- Throws:
IOException
- if unable to read the stream (or handle the charset)
-
toString
Read input stream to string.- Parameters:
in
- the reader to read from (until EOF)- Returns:
- the String parsed from the reader
- Throws:
IOException
- if unable to read the stream (or handle the charset)
-
delete
Delete File. This delete will recursively delete directories - BE CAREFUL- Parameters:
file
- The file (or directory) to be deleted.- Returns:
- true if file was deleted, or directory referenced was deleted. false if file doesn't exist, or was null.
-
delete
Delete the path, recursively.- Parameters:
path
- the path to delete- Returns:
- true if able to delete the path, false if unable to delete the path.
-
isEmptyDir
Test if directory is empty.- Parameters:
dir
- the directory- Returns:
- true if directory is null, doesn't exist, or has no content. false if not a directory, or has contents
-
close
Closes an arbitrary closable, and logs exceptions at ignore level- Parameters:
closeable
- the closeable to close
-
close
Closes an arbitrary closable, and logs exceptions at ignore level- Parameters:
closeable
- the closeable to close
-
close
closes an input stream, and logs exceptions- Parameters:
is
- the input stream to close
-
close
closes an output stream, and logs exceptions- Parameters:
os
- the output stream to close
-
close
closes a reader, and logs exceptions- Parameters:
reader
- the reader to close
-
close
closes a writer, and logs exceptions- Parameters:
writer
- the writer to close
-
readBytes
- Throws:
IOException
-
write
public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length) throws IOException A gathering write utility wrapper.This method wraps a gather write with a loop that handles the limitations of some operating systems that have a limit on the number of buffers written. The method loops on the write until either all the content is written or no progress is made.
- Parameters:
out
- The GatheringByteChannel to write tobuffers
- The buffers to writeoffset
- The offset into the buffers arraylength
- The length in buffers to write- Returns:
- The total bytes written
- Throws:
IOException
- if unable write to the GatheringByteChannel
-
asFile
Convert an object to a
File
if possible.- Parameters:
fileObject
- A File, String, Path or null to be converted into a File- Returns:
- A File representation of the passed argument or null.
-
copyDir(Path, Path)
instead to avoid foreign target behavior across FileSystems.