Dashboard Builder Commons 6.0.0.CR2

org.jboss.dashboard.commons.io
Class FileUtil

java.lang.Object
  extended by org.jboss.dashboard.commons.io.FileUtil

public class FileUtil
extends Object

Utilities to work with files.


Method Summary
static void appendToFile(byte[] contents, String filename)
          Append content to a file
static void appendToFile(String contents, String filename)
          Append content to a file
static String changeSeparator(String p)
          Changes File.separator into a Windows path to Unix path and vice versa.
static long compFileDate(String fileName1, String fileName2)
          Test if the time a file was last modified is before, after or equals than the time another file was last modified
static void copyFile(InputStream in, String destFile)
          Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
static void copyFile(InputStream in, String destFile, int copyBufferSize)
          Copy a file to another file using a buffer of maximum size
static void copyFile(String sourceFile, String destFile)
          Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
static void copyFile(String sourceFile, String destFile, int copyBufferSize)
          Copy a file to another file using a buffer of maximum size
static String createDir(String dirName)
          Creates the directory named by a pathname, if it doesn't exists, including any necessary but nonexistent parent directories.
static void deleteFile(String fileName)
          Delete the file or directory denoted by a pathname.
static boolean existFile(String fileName)
          Test whether the file denoted by a pathname exists
static String getTemporalFileName(String filePath)
          Gets a non existing filename in the directory passed as a parameter.
static boolean isDirectory(String path)
          Check if a directory exists and is a directory.
static boolean isUpdateFile(String fileName1, String fileName2)
          Test if the second file was the last modified
static void makeBackupFile(String fileName)
          Make a backup copy of a file denoted by a pathname, renaming the original file into a *.bak file.
static void moveFile(InputStream in, String destFile)
          Move a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
static void moveFile(InputStream in, String destFile, int moveBufferSize)
          Move a file to another file using a buffer of maximum size
static void moveFile(String sourceFile, String destFile)
          MOve a file to another file using a static buffer of DEFAULT_BUFFER_SIZE
static void moveFile(String sourceFile, String destFile, int moveBufferSize)
          MOve a file to another file using a buffer of maximum size
static byte[] readFile(String filename)
          Read a file into a buffer sized the size of the file
static byte[] readFromInputStream(InputStream is)
          Reads a set of bytes from an InputStream and returns the data as a byte array.
static Vector readLines(String file)
          Read all lines of text in a file.
static byte[] readResourceFile(String fileName)
          Tries to read a file from disk, if it doesn't exist try to read from the CLASSPATH.
static void renameFile(String oldFileName, String newFileName)
          Rename a file
static void replace(String file, String newfile, String[] cads, String[] newcads)
          Replace the ocurrences of a list of strings in a File with new strings
static void replace(String file, String newfile, String cad, String newcad)
          Replace the ocurrences of a string in a File with new string
static void restoreBackupFile(String fileName)
          Restore a backup copy of a file denoted by a pathname, renaming the backup file (*.bak) into a original file.
static boolean swapFile(String source, String tmp)
          From an existing source file, and an existing temporal file, copy, in a trunc way, the temporal in place of the source file, and remove the temporal file
static void writeLines(Vector lines, String file, boolean append)
          Write an array of strings to the specified file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

existFile

public static boolean existFile(String fileName)
Test whether the file denoted by a pathname exists

Parameters:
fileName - File pathname
Returns:
true if and only if the file denoted by this pathname exists; false otherwise

compFileDate

public static long compFileDate(String fileName1,
                                String fileName2)
Test if the time a file was last modified is before, after or equals than the time another file was last modified

Parameters:
fileName1 - First file pathname
fileName2 - Second file pathname
Returns:
0 if and only if the instant of time both files was last modified is the same. <0 if the second file was the last modified file and >0 if the first file was the last modified.

isUpdateFile

public static boolean isUpdateFile(String fileName1,
                                   String fileName2)
Test if the second file was the last modified

Parameters:
fileName1 - First file pathname
fileName2 - Second file pathname
Returns:
true if and only if the second file is the last modified; false otherwise

renameFile

public static void renameFile(String oldFileName,
                              String newFileName)
Rename a file

Parameters:
oldFileName - Old file name
newFileName - New file name

deleteFile

public static void deleteFile(String fileName)
Delete the file or directory denoted by a pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted

Parameters:
fileName - File pathname

replace

public static void replace(String file,
                           String newfile,
                           String cad,
                           String newcad)
                    throws IOException
Replace the ocurrences of a string in a File with new string

Parameters:
file - Source file
newfile - Destination file. If this parameter is 'null' the changes are stored in the source file.
cad - String to replace
newcad - New string
Throws:
IOException

replace

public static void replace(String file,
                           String newfile,
                           String[] cads,
                           String[] newcads)
                    throws IOException
Replace the ocurrences of a list of strings in a File with new strings

Parameters:
file - Source file
newfile - Destination file. If this parameter is 'null' the changes are stored in the source file.
cads - String list to replace
newcads - New string list
Throws:
IOException

makeBackupFile

public static void makeBackupFile(String fileName)
Make a backup copy of a file denoted by a pathname, renaming the original file into a *.bak file. If there is a previous file with these name, it is deleted

Parameters:
fileName - File pathname

restoreBackupFile

public static void restoreBackupFile(String fileName)
Restore a backup copy of a file denoted by a pathname, renaming the backup file (*.bak) into a original file. If there is a previous file with these name, it is deleted

Parameters:
fileName - File pathname

readFile

public static byte[] readFile(String filename)
                       throws FileNotFoundException,
                              IOException
Read a file into a buffer sized the size of the file

Parameters:
filename - The file full-path
Returns:
The file contents
Throws:
FileNotFoundException
IOException

readResourceFile

public static byte[] readResourceFile(String fileName)
                               throws FileNotFoundException,
                                      IOException
Tries to read a file from disk, if it doesn't exist try to read from the CLASSPATH.

Parameters:
fileName - The file full-path or file name (to search into CLASSPATH)
Returns:
The file contents
Throws:
FileNotFoundException
IOException

readFromInputStream

public static byte[] readFromInputStream(InputStream is)
                                  throws IOException
Reads a set of bytes from an InputStream and returns the data as a byte array.

Parameters:
is - InputStream
Returns:
Array of bytes
Throws:
IOException

readLines

public static Vector readLines(String file)
                        throws IOException
Read all lines of text in a file. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed

Parameters:
file - File pathname
Returns:
String vector that contains all lines
Throws:
IOException

writeLines

public static void writeLines(Vector lines,
                              String file,
                              boolean append)
                       throws IOException
Write an array of strings to the specified file. If the second argument is true, then the strings will be written to the end of the file; if it is false, the strings will be written to the beginning, and the file is truncated

Parameters:
lines - array of strings to write
file - file pathname
append - if true the lines will be written to the end of file, otherwise they will be written to the begin of file and the file is truncated.
Throws:
IOException

appendToFile

public static void appendToFile(String contents,
                                String filename)
                         throws FileNotFoundException,
                                IOException
Append content to a file

Parameters:
contents - the content
filename - the file full-path
Throws:
FileNotFoundException
IOException

appendToFile

public static void appendToFile(byte[] contents,
                                String filename)
                         throws FileNotFoundException,
                                IOException
Append content to a file

Parameters:
contents - the content
filename - the file full-path
Throws:
FileNotFoundException
IOException

swapFile

public static boolean swapFile(String source,
                               String tmp)
From an existing source file, and an existing temporal file, copy, in a trunc way, the temporal in place of the source file, and remove the temporal file

Parameters:
source - the existing source file, where to copy the information from the existing temporal file
tmp - the existing temporal file from where to copy the data into the source file
Returns:
true if the files operation have finished ok, false if not

copyFile

public static void copyFile(InputStream in,
                            String destFile)
                     throws IOException
Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE

Parameters:
in - Source data stream
destFile - Destination file
Throws:
IOException

copyFile

public static void copyFile(String sourceFile,
                            String destFile)
                     throws IOException
Copy a file to another file using a static buffer of DEFAULT_BUFFER_SIZE

Parameters:
sourceFile - Source file
destFile - Destination file
Throws:
IOException

copyFile

public static void copyFile(String sourceFile,
                            String destFile,
                            int copyBufferSize)
                     throws IOException
Copy a file to another file using a buffer of maximum size

Parameters:
sourceFile - Source file
destFile - Destination file
copyBufferSize - Maximum size of the buffer
Throws:
IOException

copyFile

public static void copyFile(InputStream in,
                            String destFile,
                            int copyBufferSize)
                     throws IOException
Copy a file to another file using a buffer of maximum size

Parameters:
in - Source data stream
destFile - Destination file
copyBufferSize - Maximum size of the buffer
Throws:
IOException

moveFile

public static void moveFile(String sourceFile,
                            String destFile)
                     throws IOException
MOve a file to another file using a static buffer of DEFAULT_BUFFER_SIZE

Parameters:
sourceFile - Source file
destFile - Destination file
Throws:
IOException

moveFile

public static void moveFile(InputStream in,
                            String destFile)
                     throws IOException
Move a file to another file using a static buffer of DEFAULT_BUFFER_SIZE

Parameters:
in - Source data stream
destFile - Destination file
Throws:
IOException

moveFile

public static void moveFile(String sourceFile,
                            String destFile,
                            int moveBufferSize)
                     throws IOException
MOve a file to another file using a buffer of maximum size

Parameters:
sourceFile - Source file
destFile - Destination file
moveBufferSize - Maximum size of the buffer
Throws:
IOException

moveFile

public static void moveFile(InputStream in,
                            String destFile,
                            int moveBufferSize)
                     throws IOException
Move a file to another file using a buffer of maximum size

Parameters:
in - Source data stream
destFile - Destination file
moveBufferSize - Maximum size of the buffer
Throws:
IOException

createDir

public static String createDir(String dirName)
Creates the directory named by a pathname, if it doesn't exists, including any necessary but nonexistent parent directories.

Parameters:
dirName - Directory pathname
Returns:
Directory name.

isDirectory

public static boolean isDirectory(String path)
Check if a directory exists and is a directory.

Parameters:
path - the path of the directory to be checked.
Returns:
true if the directory exists or false otherwise.

getTemporalFileName

public static String getTemporalFileName(String filePath)
Gets a non existing filename in the directory passed as a parameter. The file name is a long number

Parameters:
filePath - The path where is going to be placed the file.
Returns:
a non existing filename. is a long passed as a String.

changeSeparator

public static String changeSeparator(String p)
Changes File.separator into a Windows path to Unix path and vice versa.

Parameters:
p - String which contains the path
Returns:
string with the transformed path

Dashboard Builder Commons 6.0.0.CR2

Copyright © 2012-2013 JBoss by Red Hat. All Rights Reserved.