java.lang.Object
org.klojang.util.IOMethods
I/O-related methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Simple file-write method.static File
Creates a new, empty directory under the file system's temp directory.static File
createTempDir
(Class<?> requester) Creates a new, empty directory under the file system's temp directory.static File
createTempDir
(Class<?> requester, boolean touch) Creates aFile
object with a unique file name, located file system's temp directory.static File
createTempDir
(Class<?> requester, String extension, boolean touch) Creates aFile
object with a unique file name, located in file system's temp directory.static File
createTempDir
(Class<?> requester, String relativePath, String extension, boolean touch) Creates aFile
object with a unique file name, located in file system's temp directory.static File
Creates a new, empty file in the file system's temp directory.static File
createTempFile
(Class<?> requester) Creates a new, empty file in the file system's temp directory.static File
createTempFile
(Class<?> requester, boolean touch) Creates aFile
object with a unique file name, located file system's temp directory.static File
createTempFile
(Class<?> requester, String extension, boolean touch) Creates aFile
object with a unique file name, located in file system's temp directory.static File
createTempFile
(Class<?> requester, String relativePath, String extension, boolean touch) Creates aFile
object with a unique file name, located in file system's temp directory.static String
Returns aString
created from the bytes read from the specified input stream.static String
getContents
(InputStream in, int chunkSize) Returns aString
created from the bytes read from the specified input stream.static String
getContents
(Class<?> clazz, String path) Returns the contents of the specified resource.static String
getContents
(Class<?> clazz, String path, int chunkSize) Returns the contents of the specified resource.static String
getContents
(String path) Returns the contents of the specified file.static void
pipe
(InputStream in, OutputStream out) Reads all bytes from the specified input stream and writes them to the specified output stream.static void
pipe
(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 void
Deletes a file or directory.static void
Deletes a file or directory.static void
Simple file-write method.
-
Method Details
-
getContents
Returns the contents of the specified file.- Parameters:
path
- the path to the file- Returns:
- the contents of the specified file
-
getContents
Returns the contents of the specified resource. Bytes are read in chunks of 512 bytes.- Parameters:
clazz
- theClass
to callgetResourceAsStream
onpath
- the path to the resource- Returns:
- the contents of the specified resource
-
getContents
Returns the contents of the specified resource.- Parameters:
clazz
- theClass
to callgetResourceAsStream
onpath
- the path to the resourcechunkSize
- the number of bytes to read at a time- Returns:
- the contents of the specified resource
-
getContents
Returns aString
created 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
String
from the bytes read from the specified input stream
-
getContents
Returns aString
created 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
String
from the bytes read from the specified input stream
-
write
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
-
append
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
-
readBytes
Returns the contents of the specified resource as a byte array. Bytes are read in chunks of 512 bytes.- Parameters:
clazz
- theClass
to callgetResourceAsStream
onpath
- 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
- theClass
to callgetResourceAsStream
onpath
- 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
File
object 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
File
object for a new, empty file in the file system's temp directory - Throws:
IOException
- if an I/O error occurs
-
createTempFile
Creates aFile
object 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
File
object 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 aFile
object 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
File
object 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 aFile
object 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
File
object 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
File
object 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
File
object for a new, empty file in the file system's temp directory - Throws:
IOException
- if an I/O error occurs
-
createTempDir
Creates aFile
object 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
File
object 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 aFile
object with a unique file name, located in file system's temp directory. Iftouch
equalstrue
, theFile
object 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
File
object 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 aFile
object with a unique file name, located in file system's temp directory. Iftouch
equalstrue
, theFile
object 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
File
object 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.
-