Class IOMethods
java.lang.Object
org.klojang.util.IOMethods
I/O-related methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendContents(File file, byte[] contents) Simple file-write method.static voidappendContents(File file, String contents) Simple file-write method.static voidappendContents(String path, byte[] contents) Simple file-write method.static voidappendContents(String path, String contents) Simple file-write method.static voidappendContents(Path path, byte[] contents) Simple file-write method.static voidappendContents(Path path, String contents) Simple file-write method.static FileCreates a new, empty directory under the file system's temp directory.static FilecreateTempDir(Class<?> requester) Creates a new, empty directory under the file system's temp directory.static FilecreateTempDir(Class<?> requester, boolean touch) Creates aFileobject with a unique file name, located file system's temp directory.static FilecreateTempDir(Class<?> requester, String extension, boolean touch) Creates aFileobject with a unique file name, located in file system's temp directory.static FilecreateTempDir(Class<?> requester, String relativePath, String extension, boolean touch) Creates aFileobject with a unique file name, located in file system's temp directory.static FileCreates a new, empty file in the file system's temp directory.static FilecreateTempFile(Class<?> requester) Creates a new, empty file in the file system's temp directory.static FilecreateTempFile(Class<?> requester, boolean touch) Creates aFileobject with a unique file name, located file system's temp directory.static FilecreateTempFile(Class<?> requester, String extension, boolean touch) Creates aFileobject with a unique file name, located in file system's temp directory.static FilecreateTempFile(Class<?> requester, String relativePath, String extension, boolean touch) Creates aFileobject with a unique file name, located in file system's temp directory.static StringReturns aStringcreated from the bytes read from the specified input stream.static StringgetContents(InputStream in, int chunkSize) Returns aStringcreated from the bytes read from the specified input stream.static StringgetContents(Class<?> clazz, String path) Returns the contents of the specified resource.static StringgetContents(Class<?> clazz, String path, int chunkSize) Returns the contents of the specified resource.static StringgetContents(String path) Returns the contents of the specified file.static voidpipe(InputStream in, OutputStream out) Reads all bytes from the specified input stream and writes them to the specified output stream.static voidpipe(InputStream in, OutputStream out, int chunkSize) Reads all bytes from the specified input stream and writes them to the specified output stream.static byte[]readBytes(InputStream in) Returns abyte[]array containing the bytes read from the specified input stream.static byte[]readBytes(InputStream in, int chunkSize) Returns abyte[]array containing the bytes read from the specified input stream.static byte[]Returns the contents of the specified resource as a byte array.static byte[]Returns the contents of the specified resource as a byte array.static voidDeletes a file or directory.static voidDeletes a file or directory.static voidsetContents(File file, byte[] contents) Simple file-write method.static voidsetContents(File file, String contents) Simple file-write method.static voidsetContents(String path, byte[] contents) Simple file-write method.static voidsetContents(String path, String contents) Simple file-write method.static voidsetContents(Path path, byte[] contents) Simple file-write method.static voidsetContents(Path path, String contents) Simple file-write method.
-
Method Details
-
getContents
-
getContents
Returns the contents of the specified resource. Bytes are read in chunks of 512 bytes.- Parameters:
clazz- theClassto callgetResourceAsStreamonpath- the path to the resource- Returns:
- the contents of the specified resource
-
getContents
Returns the contents of the specified resource.- Parameters:
clazz- theClassto callgetResourceAsStreamonpath- the path to the resourcechunkSize- the number of bytes to read at a time- Returns:
- the contents of the specified resource
-
getContents
Returns aStringcreated from the bytes read from the specified input stream. Bytes are read in chunks of 512 bytes. The input stream is not closed once all bytes have been read.- Parameters:
in- the input stream- Returns:
- a
Stringfrom the bytes read from the specified input stream
-
getContents
Returns aStringcreated from the bytes read from the specified input stream. The input stream is not closed once all bytes have been read.- Parameters:
in- the input streamchunkSize- the number of bytes to read at a time- Returns:
- a
Stringfrom the bytes read from the specified input stream
-
setContents
-
setContents
-
setContents
-
setContents
Simple file-write method. Not efficient, but easy to use. Overwrites pre-existing contents.- Parameters:
path- the path to the filecontents- The contents to be written
-
setContents
Simple file-write method. Not efficient, but easy to use. Overwrites pre-existing contents.- Parameters:
path- the path to the filecontents- The contents to be written
-
setContents
Simple file-write method. Not efficient, but easy to use. Overwrites pre-existing contents.- Parameters:
file- the file to writecontents- The contents to be written
-
appendContents
-
appendContents
-
appendContents
-
appendContents
Simple file-write method. Not efficient, but easy to use. Appends the specified string to the contents of the specified file.- Parameters:
path- the path to the filecontents- The contents to be written
-
appendContents
Simple file-write method. Not efficient, but easy to use. Appends the specified string to the contents of the specified file.- Parameters:
path- the path to the filecontents- The contents to be written
-
appendContents
Simple file-write method. Not efficient, but easy to use. Appends the specified string to the contents of the specified file.- Parameters:
file- the file to writecontents- The contents to be written
-
readBytes
Returns the contents of the specified resource as a byte array. Bytes are read in chunks of 512 bytes.- Parameters:
clazz- theClassto callgetResourceAsStreamonpath- the path to the resource- Returns:
- the bytes contained in the specified resource
-
readBytes
Returns the contents of the specified resource as a byte array.- Parameters:
clazz- theClassto callgetResourceAsStreamonpath- the path to the resourcechunkSize- the number of bytes to read at a time- Returns:
- the contents of the specified resource
-
readBytes
Returns abyte[]array containing the bytes read from the specified input stream. Bytes are read in chunks of 512 bytes. The input stream is not closed by this method.- Parameters:
in- the input stream- Returns:
- a
byte[]array containing the bytes read from the specified input stream
-
readBytes
Returns abyte[]array containing the bytes read from the specified input stream. Bytes are read in chunks of the specified size. The input stream is not closed by this method.- Parameters:
in- the input streamchunkSize- the number of bytes to read at a time- Returns:
- a
byte[]array containing the bytes read from the specified input stream
-
pipe
Reads all bytes from the specified input stream and writes them to the specified output stream. Bytes are read and written in chunks of 512 bytes at a time. The input stream and the output stream are not closed by this method.- Parameters:
in- the input streamout- the output stream
-
pipe
Reads all bytes from the specified input stream and writes them to the specified output stream. Bytes are read and written in chunks of the specified size. The input stream and the output stream are not closed by this method.- Parameters:
in- the input streamout- the output streamchunkSize- the number of bytes read/written at a time
-
createTempFile
Creates a new, empty file in the file system's temp directory. Equivalent to:createTempFile(IOMethods.class, ".tmp", true)- Returns:
- a
Fileobject for a new, empty file in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempFile
Creates a new, empty file in the file system's temp directory. Equivalent to:createTempFile(requester ".tmp", true)- Parameters:
requester- the class requesting the temp file (its simple name will become part of the file name)- Returns:
- a
Fileobject for a new, empty file in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempFile
Creates aFileobject with a unique file name, located file system's temp directory. Equivalent to:createTempFile(requester "tmp", touch)- Parameters:
requester- the class requesting the temp file (its simple name will become part of the file name)touch- whether to actually create the file on the file system- Returns:
- a
Fileobject for a new, empty file in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempFile
public static File createTempFile(Class<?> requester, String extension, boolean touch) throws IOException Creates aFileobject with a unique file name, located in file system's temp directory.- Parameters:
requester- the class requesting the temp file (its simple name will become part of the file name)extension- the extension to append to the generated directory name. If empty ornull, no extension will be appended to the file name. Otherwise the extension may or may not start with "." (the dot will be prepended if absent).touch- whether to actually create the file on the file system- Returns:
- a
Fileobject for a new, empty file in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempFile
public static File createTempFile(Class<?> requester, String relativePath, String extension, boolean touch) throws IOException Creates aFileobject with a unique file name, located in file system's temp directory.- Parameters:
requester- the class requesting the temp directory (its simple name will become part of the file name)relativePath- any intermediate directories between the file system's temp directory and the file to be created. "/" means the file will be created directly underneath the file system's temp directory. Intermediate directories will be created if necessary. If the path does not start and/or end with "/" (File.separator), it will be prepended resp. appended.extension- the extension to append to the generated directory name. If empty ornull, no extension will be appended to the directory name. Otherwise the extension may or may not start with "." (the dot will be prepended if absent).touch- whether to actually create the directory on the file system- Returns:
- a
Fileobject for a new, empty file in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempDir
Creates a new, empty directory under the file system's temp directory. Equivalent to:createTempFile(requester ".d", true)- Returns:
- a
Fileobject for a new, empty directory in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempDir
Creates a new, empty directory under the file system's temp directory. Equivalent to:createTempFile(requester, ".d", true)- Parameters:
requester- the class requesting the temp file (its simple name will become part of the file name)- Returns:
- a
Fileobject for a new, empty file in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempDir
Creates aFileobject with a unique file name, located file system's temp directory. Equivalent to:createTempDir(requester ".d", touch)- Parameters:
requester- the class requesting the temp directory (its simple name will become part of the file name)touch- whether to actually create the directory on the file system- Returns:
- a
Fileobject for a new, empty directory in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempDir
public static File createTempDir(Class<?> requester, String extension, boolean touch) throws IOException Creates aFileobject with a unique file name, located in file system's temp directory. Iftouchequalstrue, theFileobject will be used to create a directory underneath the temp directory.- Parameters:
requester- the class requesting the temp directory (its simple name will become part of the file name)extension- the extension to append to the generated directory name. If empty ornull, no extension will be appended to the directory name. Otherwise the extension may or may not start with "." (the dot will be prepended if absent).touch- whether to actually create the directory on the file system- Returns:
- a
Fileobject for a new, empty directory in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
createTempDir
public static File createTempDir(Class<?> requester, String relativePath, String extension, boolean touch) throws IOException Creates aFileobject with a unique file name, located in file system's temp directory. Iftouchequalstrue, theFileobject will be used to create a directory underneath the temp directory.- Parameters:
requester- the class requesting the temp directory (its simple name will become part of the file name)relativePath- any intermediate directories between the file system's temp directory and the directory to be created. "/" means the directory will be created directly underneath the file system's temp directory. Intermediate directories will be created if necessary. If the path does not start or end with "/" (File.separator), it will be prepended resp. appended.extension- the extension to append to the generated directory name. If empty ornull, no extension will be appended to the directory name. Otherwise the extension may or may not start with "." (the dot will be prepended if absent).touch- whether to actually create the directory on the file system- Returns:
- a
Fileobject for a new, empty directory in the file system's temp directory - Throws:
IOException- if an I/O error occurs
-
rm
Deletes a file or directory. Directories need not be empty. If the file or directory does not exist, this method returns quietly.- Parameters:
path- the path of the file/directory to be deleted
-
rm
Deletes a file or directory. Directories need not be empty. If the file or directory does not exist, this method returns quietly.- Parameters:
file- The file or directory to delete.
-