Package com.itextpdf.commons.utils
Class FileUtil
- java.lang.Object
-
- com.itextpdf.commons.utils.FileUtil
-
public final class FileUtil extends java.lang.ObjectThis file is a helper class for internal usage only. Be aware that its API and functionality may be changed in future.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classFileUtil.CaseSensitiveFileComparator
-
Constructor Summary
Constructors Modifier Constructor Description privateFileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.io.FileconstructFileByDirectoryAndName(java.lang.String directory, java.lang.String fileName)CreateFileinstance.static voidcopy(java.lang.String inputFile, java.lang.String outputFile)Creates a copy of a file.static voidcreateDirectories(java.lang.String outPath)Creates a directory at the provided path.static java.io.PrintWritercreatePrintWriter(java.io.OutputStream output, java.lang.String encoding)CreatesPrintWriterinstance.static java.lang.StringcreateTempCopy(java.lang.String file, java.lang.String tempFilePrefix, java.lang.String tempFilePostfix)Creates a temporary copy of a file.static java.lang.StringcreateTempDirectory(java.lang.String tempFilePrefix)Creates a temporary directory.static java.io.FilecreateTempFile(java.lang.String path)Creates a temporary file at the provided path.static java.io.FilecreateTempFile(java.lang.String tempFilePrefix, java.lang.String tempFilePostfix)Creates a temporary file.static booleandeleteFile(java.io.File file)Deletes a file and returns whether the operation succeeded.static booleandirectoryExists(java.lang.String path)Checks whether there is a directory at the provided path.static booleanfileExists(java.lang.String path)Checks whether there is a file at the provided path.static java.io.OutputStreamgetBufferedOutputStream(java.lang.String filename)CreatesOutputStreaminstance for filename.static java.io.OutputStreamgetFileOutputStream(java.io.File tempFile)CreatesFileOutputStreaminstance.static java.io.OutputStreamgetFileOutputStream(java.lang.String path)CreatesOutputStreaminstance.static java.lang.StringgetFontsDir()Gets the default windows font directory.static java.io.InputStreamgetInputStreamForFile(java.io.File file)CreatesInputStreaminstance.static java.io.InputStreamgetInputStreamForFile(java.lang.String path)CreatesInputStreaminstance.static java.lang.StringgetParentDirectoryUri(java.io.File file)Returns an URI of the parent directory for the resource.static java.io.RandomAccessFilegetRandomAccessFile(java.io.File tempFile)CreatesRandomAccessFileinstance.static booleanisFileNotEmpty(java.lang.String path)Checks whether is provided file not empty.private static voidlistAllFiles(java.lang.String dir, java.util.List<java.lang.String> list)static java.lang.String[]listFilesInDirectory(java.lang.String path, boolean recursive)Lists all the files located at the provided directory.static java.io.File[]listFilesInDirectoryByFilter(java.lang.String outPath, java.io.FileFilter fileFilter)Lists all the files located at the provided directory, which are accepted by the provided filter.static java.lang.StringparentDirectory(java.net.URL url)Returns an URL of the parent directory for the resource.static booleanremoveFiles(java.lang.String[] paths)Removes all of the passed files.static java.io.OutputStreamwrapWithBufferedOutputStream(java.io.OutputStream outputStream)Wraps provided output stream with buffered one.
-
-
-
Method Detail
-
getFontsDir
public static java.lang.String getFontsDir()
Gets the default windows font directory.- Returns:
- the default windows font directory
-
fileExists
public static boolean fileExists(java.lang.String path)
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
public static boolean isFileNotEmpty(java.lang.String path)
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
public static boolean directoryExists(java.lang.String path)
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
public static java.lang.String[] listFilesInDirectory(java.lang.String path, boolean recursive)Lists all the files located at the provided directory.- Parameters:
path- path to the directoryrecursive- iftrue, files from all the subdirectories will be returned- Returns:
- all the files located at the provided directory
-
listFilesInDirectoryByFilter
public static java.io.File[] listFilesInDirectoryByFilter(java.lang.String outPath, java.io.FileFilter fileFilter)Lists all the files located at the provided directory, which are accepted by the provided filter.- Parameters:
outPath- path to the directoryfileFilter- filter to accept files to be listed- Returns:
- all the files located at the provided directory, which are accepted by the provided filter
-
listAllFiles
private static void listAllFiles(java.lang.String dir, java.util.List<java.lang.String> list)
-
createPrintWriter
public static java.io.PrintWriter createPrintWriter(java.io.OutputStream output, java.lang.String encoding) throws java.io.UnsupportedEncodingExceptionCreatesPrintWriterinstance.- Parameters:
output- output stream where data will be written.encoding- encoding in which data will be written.- Returns:
PrintWriterinstance.- Throws:
java.io.UnsupportedEncodingException- in case of unknown encoding.
-
getBufferedOutputStream
public static java.io.OutputStream getBufferedOutputStream(java.lang.String filename) throws java.io.IOExceptionCreatesOutputStreaminstance for filename.- Parameters:
filename- name of the file for which output stream will be created.- Returns:
OutputStreaminstance.- Throws:
java.io.FileNotFoundException- if file not found.java.io.IOException
-
wrapWithBufferedOutputStream
public static java.io.OutputStream wrapWithBufferedOutputStream(java.io.OutputStream outputStream)
Wraps provided output stream with buffered one.- Parameters:
outputStream- output stream to wrap.- Returns:
BufferedOutputStreaminstance if provided stream was not buffered before.
-
constructFileByDirectoryAndName
public static java.io.File constructFileByDirectoryAndName(java.lang.String directory, java.lang.String fileName)CreateFileinstance.- Parameters:
directory- the parent pathname string.fileName- The child pathname string.- Returns:
Fileinstance.
-
createTempFile
public static java.io.File createTempFile(java.lang.String path) throws java.io.IOExceptionCreates 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:
java.io.IOException- signals that an I/O exception has occurred
-
getFileOutputStream
public static java.io.OutputStream getFileOutputStream(java.io.File tempFile) throws java.io.IOExceptionCreatesFileOutputStreaminstance.- Parameters:
tempFile- filename for which output stream will be created.- Returns:
FileOutputStreaminstance.- Throws:
java.io.IOException- in file reading errors.
-
getInputStreamForFile
public static java.io.InputStream getInputStreamForFile(java.lang.String path) throws java.io.IOExceptionCreatesInputStreaminstance.- Parameters:
path- filename for which output stream will be created.- Returns:
InputStreaminstance.- Throws:
java.io.IOException- in file reading errors.
-
getInputStreamForFile
public static java.io.InputStream getInputStreamForFile(java.io.File file) throws java.io.IOExceptionCreatesInputStreaminstance.- Parameters:
file- filename for which output stream will be created.- Returns:
InputStreaminstance.- Throws:
java.io.IOException- in file reading errors.
-
getFileOutputStream
public static java.io.OutputStream getFileOutputStream(java.lang.String path) throws java.io.IOExceptionCreatesOutputStreaminstance.- Parameters:
path- filename for which output stream will be created.- Returns:
OutputStreaminstance.- Throws:
java.io.IOException- in file r/w errors.
-
getRandomAccessFile
public static java.io.RandomAccessFile getRandomAccessFile(java.io.File tempFile) throws java.io.FileNotFoundExceptionCreatesRandomAccessFileinstance.- Parameters:
tempFile- file for which RAF will be created.- Returns:
RandomAccessFileinstance.- Throws:
java.io.FileNotFoundException- in case file not found.
-
createDirectories
public static void createDirectories(java.lang.String outPath)
Creates a directory at the provided path.- Parameters:
outPath- path to the directory to be created
-
getParentDirectoryUri
public static java.lang.String getParentDirectoryUri(java.io.File file) throws java.net.MalformedURLExceptionReturns 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:
java.net.MalformedURLException- If a protocol handler for the URL could not be found, or if some other error occurred while constructing the URL.
-
deleteFile
public static boolean deleteFile(java.io.File file)
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
public static java.lang.String parentDirectory(java.net.URL url) throws java.net.URISyntaxExceptionReturns 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:
java.net.URISyntaxException- if this URL is not formatted strictly according to RFC2396 and cannot be converted to a URI.
-
createTempFile
public static java.io.File createTempFile(java.lang.String tempFilePrefix, java.lang.String tempFilePostfix) throws java.io.IOExceptionCreates 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 nametempFilePostfix- the postfix of the copied file's name- Returns:
- the path to the copied file
- Throws:
java.io.IOException- signals that an I/O exception has occurred.
-
createTempCopy
public static java.lang.String createTempCopy(java.lang.String file, java.lang.String tempFilePrefix, java.lang.String tempFilePostfix) throws java.io.IOExceptionCreates 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 copiedtempFilePrefix- the prefix of the copied file's nametempFilePostfix- the postfix of the copied file's name- Returns:
- the path to the copied file
- Throws:
java.io.IOException- signals that an I/O exception has occurred.
-
copy
public static void copy(java.lang.String inputFile, java.lang.String outputFile) throws java.io.IOExceptionCreates a copy of a file.- Parameters:
inputFile- the path to the file to be copiedoutputFile- the path, to which the passed file should be copied- Throws:
java.io.IOException- signals that an I/O exception has occurred.
-
createTempDirectory
public static java.lang.String createTempDirectory(java.lang.String tempFilePrefix) throws java.io.IOExceptionCreates 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:
java.io.IOException- signals that an I/O exception has occurred.
-
removeFiles
public static boolean removeFiles(java.lang.String[] paths)
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
-
-