Package com.itextpdf.commons.utils
Class FileUtil
java.lang.Object
com.itextpdf.commons.utils.FileUtil
This file is a helper class for internal usage only. Be aware that its API and functionality may be changed in future.
-
Method Summary
Modifier and TypeMethodDescriptionstatic FileconstructFileByDirectoryAndName(String directory, String fileName) CreateFileinstance.static voidCreates a copy of a file.static voidcreateDirectories(String outPath) Creates a directory at the provided path.static PrintWritercreatePrintWriter(OutputStream output, String encoding) CreatesPrintWriterinstance.static StringcreateTempCopy(String file, String tempFilePrefix, String tempFilePostfix) Creates a temporary copy of a file.static StringcreateTempDirectory(String tempFilePrefix) Creates a temporary directory.static FilecreateTempFile(String path) Creates a temporary file at the provided path.static FilecreateTempFile(String tempFilePrefix, String tempFilePostfix) Creates a temporary file.static booleandeleteFile(File file) Deletes a file and returns whether the operation succeeded.static booleandirectoryExists(String path) Checks whether there is a directory at the provided path.static booleanfileExists(String path) Checks whether there is a file at the provided path.static OutputStreamgetBufferedOutputStream(String filename) CreatesOutputStreaminstance for filename.static OutputStreamgetFileOutputStream(File tempFile) CreatesFileOutputStreaminstance.static OutputStreamgetFileOutputStream(String path) CreatesOutputStreaminstance.static StringGets the default windows font directory.static InputStreamgetInputStreamForFile(File file) CreatesInputStreaminstance.static InputStreamgetInputStreamForFile(String path) CreatesInputStreaminstance.static StringgetParentDirectoryUri(File file) Returns an URI of the parent directory for the resource.static RandomAccessFilegetRandomAccessFile(File tempFile) CreatesRandomAccessFileinstance.static booleanisFileNotEmpty(String path) Checks whether is provided file not empty.static String[]listFilesInDirectory(String path, boolean recursive) Lists all the files located at the provided directory.static File[]listFilesInDirectoryByFilter(String outPath, FileFilter fileFilter) Lists all the files located at the provided directory, which are accepted by the provided filter.static StringparentDirectory(URL url) Returns an URL of the parent directory for the resource.static booleanremoveFiles(String[] paths) Removes all of the passed files.static OutputStreamwrapWithBufferedOutputStream(OutputStream outputStream) Wraps provided output stream with buffered one.
-
Method Details
-
getFontsDir
Gets the default windows font directory.- Returns:
- the default windows font directory
-
fileExists
Checks whether there is a file at the provided path.- Parameters:
-
path- the path to the file to be checked on existence - Returns:
-
trueif such a file exists, otherwisefalse
-
isFileNotEmpty
Checks whether is provided file not empty.- Parameters:
-
path- the path to the file to be checked on emptiness - Returns:
-
trueif such file is not empty,falseotherwise
-
directoryExists
Checks whether there is a directory at the provided path.- Parameters:
-
path- the path to the directory to be checked on existence - Returns:
-
trueif such a directory exists, otherwisefalse
-
listFilesInDirectory
Lists all the files located at the provided directory.- Parameters:
-
path- path to the directory -
recursive- iftrue, files from all the subdirectories will be returned - Returns:
- all the files located at the provided directory
-
listFilesInDirectoryByFilter
Lists all the files located at the provided directory, which are accepted by the provided filter.- Parameters:
-
outPath- path to the directory -
fileFilter- filter to accept files to be listed - Returns:
- all the files located at the provided directory, which are accepted by the provided filter
-
createPrintWriter
public static PrintWriter createPrintWriter(OutputStream output, String encoding) throws UnsupportedEncodingException CreatesPrintWriterinstance.- Parameters:
-
output- output stream where data will be written. -
encoding- encoding in which data will be written. - Returns:
-
PrintWriterinstance. - Throws:
-
UnsupportedEncodingException- in case of unknown encoding.
-
getBufferedOutputStream
CreatesOutputStreaminstance for filename.- Parameters:
-
filename- name of the file for which output stream will be created. - Returns:
-
OutputStreaminstance. - Throws:
-
FileNotFoundException- if file not found. -
IOException
-
wrapWithBufferedOutputStream
Wraps provided output stream with buffered one.- Parameters:
-
outputStream- output stream to wrap. - Returns:
-
BufferedOutputStreaminstance if provided stream was not buffered before.
-
constructFileByDirectoryAndName
CreateFileinstance.- Parameters:
-
directory- the parent pathname string. -
fileName- The child pathname string. - Returns:
-
Fileinstance.
-
createTempFile
Creates a temporary file at the provided path.- Parameters:
-
path- path to the temporary file to be created. If it is a directory, then the temporary file will be created at this directory - Returns:
- the created temporary file
- Throws:
-
IOException- signals that an I/O exception has occurred
-
getFileOutputStream
CreatesFileOutputStreaminstance.- Parameters:
-
tempFile- filename for which output stream will be created. - Returns:
-
FileOutputStreaminstance. - Throws:
-
IOException- in file reading errors.
-
getInputStreamForFile
CreatesInputStreaminstance.- Parameters:
-
path- filename for which output stream will be created. - Returns:
-
InputStreaminstance. - Throws:
-
IOException- in file reading errors.
-
getInputStreamForFile
CreatesInputStreaminstance.- Parameters:
-
file- filename for which output stream will be created. - Returns:
-
InputStreaminstance. - Throws:
-
IOException- in file reading errors.
-
getFileOutputStream
CreatesOutputStreaminstance.- Parameters:
-
path- filename for which output stream will be created. - Returns:
-
OutputStreaminstance. - Throws:
-
IOException- in file r/w errors.
-
getRandomAccessFile
CreatesRandomAccessFileinstance.- Parameters:
-
tempFile- file for which RAF will be created. - Returns:
-
RandomAccessFileinstance. - Throws:
-
FileNotFoundException- in case file not found.
-
createDirectories
Creates a directory at the provided path.- Parameters:
-
outPath- path to the directory to be created
-
getParentDirectoryUri
Returns an URI of the parent directory for the resource.- Parameters:
-
file- for which an URI of the parent will be constructed. - Returns:
- parent directory URI.
- Throws:
-
MalformedURLException- If a protocol handler for the URL could not be found, or if some other error occurred while constructing the URL.
-
deleteFile
Deletes a file and returns whether the operation succeeded. Note that only *files* are supported, not directories.- Parameters:
-
file- file to be deleted - Returns:
- true if file was deleted successfully, false otherwise
-
parentDirectory
Returns an URL of the parent directory for the resource.- Parameters:
-
url- of resource - Returns:
- parent directory path| the same path if a catalog`s url is passed;
- Throws:
-
URISyntaxException- if this URL is not formatted strictly according to RFC2396 and cannot be converted to a URI.
-
createTempFile
public static File createTempFile(String tempFilePrefix, String tempFilePostfix) throws IOException Creates a temporary file.Note, that this method creates temporary file with provided file's prefix and postfix using
File.createTempFile(String, String).- Parameters:
-
tempFilePrefix- the prefix of the copied file's name -
tempFilePostfix- the postfix of the copied file's name - Returns:
- the path to the copied file
- Throws:
-
IOException- signals that an I/O exception has occurred.
-
createTempCopy
public static String createTempCopy(String file, String tempFilePrefix, String tempFilePostfix) throws IOException Creates a temporary copy of a file.Note, that this method creates temporary file with provided file's prefix and postfix using
Files.createTempFile(String, String, FileAttribute[]).- Parameters:
-
file- the path to the file to be copied -
tempFilePrefix- the prefix of the copied file's name -
tempFilePostfix- the postfix of the copied file's name - Returns:
- the path to the copied file
- Throws:
-
IOException- signals that an I/O exception has occurred.
-
copy
Creates a copy of a file.- Parameters:
-
inputFile- the path to the file to be copied -
outputFile- the path, to which the passed file should be copied - Throws:
-
IOException- signals that an I/O exception has occurred.
-
createTempDirectory
Creates a temporary directory.Note, that this method creates temporary directory with provided directory prefix using
Files.createTempDirectory(String, FileAttribute[]).- Parameters:
-
tempFilePrefix- the prefix of the temporary directory's name - Returns:
- the path to the temporary directory
- Throws:
-
IOException- signals that an I/O exception has occurred.
-
removeFiles
Removes all of the passed files.- Parameters:
-
paths- paths to files, which should be removed - Returns:
-
trueif all the files have been successfully removed,falseotherwise
-