Package toxi.util
Class FileUtils
java.lang.Object
toxi.util.FileUtils
A collection of file handling utilities.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
FileDialog
constantstatic final int
FileDialog
constant -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
createDirectories
(File path) Attempts to create the full path of directories as specified by the given target path.static boolean
createDirectoriesForFile
(File file) Attempts to create the full path of directories as specified by the given target file.static InputStream
createInputStream
(File file) Creates anInputStream
for the given file.static OutputStream
createOutputStream
(File file) Creates anOutputStream
for the given file.static BufferedReader
createReader
(File file) Creates aBufferedReader
for the given file using UTF-8 encoding.static BufferedReader
createReader
(InputStream input) Creates aBufferedReader
for the givenInputStream
using UTF-8 encoding.static BufferedReader
createReader
(InputStream input, String encoding) Creates aBufferedReader
for the givenInputStream
and using the specified encoding.static BufferedWriter
createWriter
(File file) Creates aBufferedWriter
for the given file using UTF-8 encoding.static BufferedWriter
createWriter
(OutputStream out) Creates aBufferedWriter
for the givenOutputStream
using UTF-8 encoding.static BufferedWriter
createWriter
(OutputStream out, String encoding) Creates aBufferedWriter
for the givenOutputStream
and using the specified encoding.static FileSequenceDescriptor
Analyses the given file path for a file sequence pattern and returns aFileSequenceDescriptor
instance for further use to handle this sequence.static byte[]
loadBytes
(InputStream stream) Loads the givenInputStream
into a byte array buffer.static String
loadText
(BufferedReader reader) Loads the givenBufferedReader
as text, line by line into aString
.static String
loadText
(InputStream input) Loads the givenInputStream
as text, line by line into aString
using UTF-8 encoding.static String
loadText
(InputStream input, String encoding) Loads the givenInputStream
as text, line by line into aString
using the specified encoding.static void
saveText
(BufferedWriter writer, String string) Saves the given text to the specifiedBufferedWriter
instance, then closes the writer afterwards.static void
saveText
(OutputStream output, String string) Saves the given text to the specifiedOutputStream
instance, then closes the underlying writer afterwards.static String
showDirectoryChooser
(Frame frame, String title, String path) Displays a standard AWT file dialog for choosing a folder (no files!).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.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.
-
Field Details
-
LOAD
public static final int LOADFileDialog
constant- See Also:
-
SAVE
public static final int SAVEFileDialog
constant- 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 anInputStream
for the given file. If the file extension ends with ".gz" the stream is automatically wrapped in aGZIPInputStream
as well.- Parameters:
file
- input file- Returns:
- input stream
- Throws:
IOException
-
createOutputStream
Creates anOutputStream
for the given file. If the file extension ends with ".gz" the stream is automatically wrapped in aGZIPOutputStream
as well. Also attempts to create any intermediate directories for the file usingcreateDirectoriesForFile(File)
.- Parameters:
file
- output file- Returns:
- output stream
- Throws:
IOException
-
createReader
Creates aBufferedReader
for the given file using UTF-8 encoding.- Parameters:
file
-- Returns:
- reader instance
- Throws:
IOException
-
createReader
Creates aBufferedReader
for the givenInputStream
using UTF-8 encoding.- Parameters:
input
- stream- Returns:
- reader instance
- Throws:
IOException
-
createReader
Creates aBufferedReader
for the givenInputStream
and using the specified encoding.- Parameters:
input
- streamencoding
- text encoding to use- Returns:
- reader instance
- Throws:
IOException
-
createWriter
Creates aBufferedWriter
for the given file using UTF-8 encoding.- Parameters:
file
-- Returns:
- writer instance
- Throws:
IOException
-
createWriter
Creates aBufferedWriter
for the givenOutputStream
using UTF-8 encoding.- Parameters:
out
-- Returns:
- writer instance
- Throws:
IOException
-
createWriter
Creates aBufferedWriter
for the givenOutputStream
and 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
FileSequenceDescriptor
instance 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 givenInputStream
into a byte array buffer.- Parameters:
stream
-- Returns:
- byte array
- Throws:
IOException
-
loadText
Loads the givenBufferedReader
as text, line by line into aString
.- Parameters:
reader
-- Returns:
- reader contents as string
- Throws:
IOException
-
loadText
Loads the givenInputStream
as text, line by line into aString
using UTF-8 encoding.- Parameters:
input
- stream- Returns:
- stream contents as string
- Throws:
IOException
-
loadText
Loads the givenInputStream
as text, line by line into aString
using the specified encoding.- Parameters:
input
- streamencoding
-- Returns:
- stream contents as single string
- Throws:
IOException
-
saveText
Saves the given text to the specifiedBufferedWriter
instance, then closes the writer afterwards.- Parameters:
writer
-string
-- Throws:
IOException
-
saveText
Saves the given text to the specifiedOutputStream
instance, 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
-