Class IO
- 
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final Stringstatic final byte[]static final int - 
Method Summary
Modifier and TypeMethodDescriptionstatic FileConvert an object to aFileif possible.static voidCloses an arbitrary closable, and logs exceptions at ignore levelstatic voidclose(InputStream is) closes an input stream, and logs exceptionsstatic voidclose(OutputStream os) closes an output stream, and logs exceptionsstatic voidcloses a reader, and logs exceptionsstatic voidcloses a writer, and logs exceptionsstatic voidclose(AutoCloseable closeable) Closes an arbitrary closable, and logs exceptions at ignore levelstatic voidCopy files or directoriesstatic voidcopy(InputStream in, OutputStream out) Copy Stream in to Stream out until EOF or exception.static voidcopy(InputStream in, OutputStream out, long byteCount) Copy Stream in to Stream for byteCount bytes or until EOF or exception.static voidCopy Reader to Writer out until EOF or exception.static voidCopy Reader to Writer for byteCount bytes or until EOF or exception.static voidstatic voidCopy the contents of a source directory to destination directory.static voidcopyDir(Path srcDir, Path destDir, CopyOption... copyOptions) Deprecated, for removal: This API element is subject to removal in a future version.static voidCopy the contents of a source file to destination file.static voidCopy the contents of a source file to destination file.static booleanDelete File.static booleanDelete the path, recursively.static voidensureDirExists(Path dir) Ensure that the given path exists, and is a directory.static booleanisEmptyDir(File dir) Test if directory is empty.static byte[]readBytes(InputStream in) static PathresolvePath(Path basePath, Path relative) Perform a resolve of abasePathPathagainst arelativePathin a way that ignoresFileSystemdifferences between the twoPathparameters.static IOExceptionstatic StringtoString(InputStream in) Read input stream to string.static StringtoString(InputStream in, String encoding) Read input stream to string.static StringtoString(InputStream in, Charset encoding) Read input stream to string.static StringRead input stream to string.static StringRead Path to string.static longwrite(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length) A gathering write utility wrapper. 
- 
Field Details
- 
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE- See Also:
 
 - 
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 theFileSystemtypes 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 abasePathPathagainst arelativePathin a way that ignoresFileSystemdifferences between the twoPathparameters.This implementation is intended to be a replacement for
Path.resolve(Path)in cases where the theFileSystemmight be different, avoiding aProviderMismatchExceptionfrom 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 thedirexists, 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
srcFileto thedestFileusingFiles.copy(Path, OutputStream). ThedestFileis opened with theOpenOptionofStandardOpenOption.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 thesrcFileanddestFileare on differentFileSystems) 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
fromFileto thetoFileusing standardInputStream/OutputStreambehaviors.- 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
Fileif possible. 
 - 
 
copyDir(Path, Path)instead to avoid foreign target behavior across FileSystems.