Package toxi.util
Class FileUtils
java.lang.Object
toxi.util.FileUtils
A collection of file handling utilities.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intFileDialogconstantstatic final intFileDialogconstant -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancreateDirectories(File path) Attempts to create the full path of directories as specified by the given target path.static booleancreateDirectoriesForFile(File file) Attempts to create the full path of directories as specified by the given target file.static InputStreamcreateInputStream(File file) Creates anInputStreamfor the given file.static OutputStreamcreateOutputStream(File file) Creates anOutputStreamfor the given file.static BufferedReadercreateReader(File file) Creates aBufferedReaderfor the given file using UTF-8 encoding.static BufferedReadercreateReader(InputStream input) Creates aBufferedReaderfor the givenInputStreamusing UTF-8 encoding.static BufferedReadercreateReader(InputStream input, String encoding) Creates aBufferedReaderfor the givenInputStreamand using the specified encoding.static BufferedWritercreateWriter(File file) Creates aBufferedWriterfor the given file using UTF-8 encoding.static BufferedWritercreateWriter(OutputStream out) Creates aBufferedWriterfor the givenOutputStreamusing UTF-8 encoding.static BufferedWritercreateWriter(OutputStream out, String encoding) Creates aBufferedWriterfor the givenOutputStreamand using the specified encoding.static FileSequenceDescriptorAnalyses the given file path for a file sequence pattern and returns aFileSequenceDescriptorinstance for further use to handle this sequence.static byte[]loadBytes(InputStream stream) Loads the givenInputStreaminto a byte array buffer.static StringloadText(BufferedReader reader) Loads the givenBufferedReaderas text, line by line into aString.static StringloadText(InputStream input) Loads the givenInputStreamas text, line by line into aStringusing UTF-8 encoding.static StringloadText(InputStream input, String encoding) Loads the givenInputStreamas text, line by line into aStringusing the specified encoding.static voidsaveText(BufferedWriter writer, String string) Saves the given text to the specifiedBufferedWriterinstance, then closes the writer afterwards.static voidsaveText(OutputStream output, String string) Saves the given text to the specifiedOutputStreaminstance, then closes the underlying writer afterwards.static StringshowDirectoryChooser(Frame frame, String title, String path) Displays a standard AWT file dialog for choosing a folder (no files!).static StringshowFileDialog(Frame frame, String title, String path, FilenameFilter filter, int mode) Displays a standard AWT file dialog for choosing a file for loading or saving.static StringshowFileDialog(Frame frame, String title, String path, String[] formats, int mode) Displays a standard AWT file dialog for choosing a file for loading or saving.
-
Field Details
-
LOAD
public static final int LOADFileDialogconstant- See Also:
-
SAVE
public static final int SAVEFileDialogconstant- See Also:
-
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
createDirectories
Attempts to create the full path of directories as specified by the given target path. The path is assumed to be a directory, NOT a file in a directory. For the latter, usecreateDirectoriesForFile(File).- Parameters:
path-- Returns:
- true, if the operation succeeded
-
createDirectoriesForFile
Attempts to create the full path of directories as specified by the given target file.- Parameters:
file-- Returns:
- true, if the operation succeeded
-
createInputStream
Creates anInputStreamfor the given file. If the file extension ends with ".gz" the stream is automatically wrapped in aGZIPInputStreamas well.- Parameters:
file- input file- Returns:
- input stream
- Throws:
IOException
-
createOutputStream
Creates anOutputStreamfor the given file. If the file extension ends with ".gz" the stream is automatically wrapped in aGZIPOutputStreamas well. Also attempts to create any intermediate directories for the file usingcreateDirectoriesForFile(File).- Parameters:
file- output file- Returns:
- output stream
- Throws:
IOException
-
createReader
Creates aBufferedReaderfor the given file using UTF-8 encoding.- Parameters:
file-- Returns:
- reader instance
- Throws:
IOException
-
createReader
Creates aBufferedReaderfor the givenInputStreamusing UTF-8 encoding.- Parameters:
input- stream- Returns:
- reader instance
- Throws:
IOException
-
createReader
Creates aBufferedReaderfor the givenInputStreamand using the specified encoding.- Parameters:
input- streamencoding- text encoding to use- Returns:
- reader instance
- Throws:
IOException
-
createWriter
Creates aBufferedWriterfor the given file using UTF-8 encoding.- Parameters:
file-- Returns:
- writer instance
- Throws:
IOException
-
createWriter
Creates aBufferedWriterfor the givenOutputStreamusing UTF-8 encoding.- Parameters:
out-- Returns:
- writer instance
- Throws:
IOException
-
createWriter
Creates aBufferedWriterfor the givenOutputStreamand using the specified encoding.- Parameters:
out- streamencoding- text encoding to use- Returns:
- writer instance
- Throws:
IOException
-
getFileSequenceDescriptorFor
Analyses the given file path for a file sequence pattern and returns a
FileSequenceDescriptorinstance for further use to handle this sequence. The file pattern should be in one of these formats:- base_path-00001.ext
- base_path001.ext
The sequence index should be using leading zeros, but the number of digits will be identified automatically.
- Parameters:
path- file path of the first file in the sequence- Returns:
- descriptor, or null, if the path could not be analysed
-
loadBytes
Loads the givenInputStreaminto a byte array buffer.- Parameters:
stream-- Returns:
- byte array
- Throws:
IOException
-
loadText
Loads the givenBufferedReaderas text, line by line into aString.- Parameters:
reader-- Returns:
- reader contents as string
- Throws:
IOException
-
loadText
Loads the givenInputStreamas text, line by line into aStringusing UTF-8 encoding.- Parameters:
input- stream- Returns:
- stream contents as string
- Throws:
IOException
-
loadText
Loads the givenInputStreamas text, line by line into aStringusing the specified encoding.- Parameters:
input- streamencoding-- Returns:
- stream contents as single string
- Throws:
IOException
-
saveText
Saves the given text to the specifiedBufferedWriterinstance, then closes the writer afterwards.- Parameters:
writer-string-- Throws:
IOException
-
saveText
Saves the given text to the specifiedOutputStreaminstance, then closes the underlying writer afterwards.- Parameters:
output-string-- Throws:
IOException
-
showDirectoryChooser
Displays a standard AWT file dialog for choosing a folder (no files!).- Parameters:
frame- parent frametitle- dialog titlepath- base directory (or null)- Returns:
- path to chosen directory or null, if user has canceled
-
showFileDialog
public static String showFileDialog(Frame frame, String title, String path, FilenameFilter filter, int mode) Displays a standard AWT file dialog for choosing a file for loading or saving.- Parameters:
frame- parent frametitle- dialog titlepath- base directory (or null)filter- a FilenameFilter implementation (or null)mode- either FileUtils.LOAD or FileUtils.SAVE- Returns:
- path to chosen file or null, if user has canceled
-
showFileDialog
public static String showFileDialog(Frame frame, String title, String path, String[] formats, int mode) Displays a standard AWT file dialog for choosing a file for loading or saving.- Parameters:
frame- parent frametitle- dialog titlepath- base directory (or null)formats- an array of allowed file extensions (or null to allow all)mode- either FileUtils.LOAD or FileUtils.SAVE- Returns:
- path to chosen file or null, if user has canceled
-