org.clapper.util.io
Class FileUtil
- java.lang.Object
-
- org.clapper.util.io.FileUtil
-
public class FileUtil extends java.lang.ObjectStatic class containing miscellaneous file utility methods.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static java.lang.Stringbasename(java.io.File file)Get the base (i.e., simple file) name of a file.static java.lang.Stringbasename(java.lang.String fileName)Get the base (i.e., simple file) name of a file.static intcopyFile(java.io.File src, java.io.File dst)Copy one file to another.static intcopyReader(java.io.Reader reader, java.io.Writer writer)Copy characters from a reader to a writer, using a default buffer size.static intcopyReader(java.io.Reader reader, java.io.Writer writer, int bufferSize)Copy characters from a reader to a writer.static intcopyStream(java.io.InputStream is, java.io.OutputStream os)Copy an InputStream to an OutputStream.static intcopyStream(java.io.InputStream src, java.io.OutputStream dst, int bufferSize)Copy an InputStream to an OutputStream.static intcopyTextFile(java.io.File src, java.lang.String srcEncoding, java.io.File dst, java.lang.String dstEncoding)Copy one file to another, character by character, possibly doing character set conversionsstatic java.lang.Stringdirname(java.io.File file)Get the name of a file's parent directory.static java.lang.Stringdirname(java.lang.String fileName)Get the name of a file's parent directory.static java.lang.StringgetDefaultEncoding()Get the virtual machine's default encoding.static java.lang.StringgetFileNameExtension(java.io.File file)Get the extension for a path or file name.static java.lang.StringgetFileNameExtension(java.lang.String path)Get the extension for a path or file name.static java.lang.StringgetFileNameNoExtension(java.io.File file)Get the name of a file without its extension.static java.lang.StringgetFileNameNoExtension(java.lang.String path)Get the name of a file without its extension.static booleanisAbsolutePath(java.lang.String path)Determine whether a string represents an absolute path.
-
-
-
Method Detail
-
isAbsolutePath
public static boolean isAbsolutePath(java.lang.String path) throws java.io.IOExceptionDetermine whether a string represents an absolute path. On Unix, an absolute path must start with a "/". On Windows, it must begin with one of the machine's valid drive letters.- Parameters:
path- the path to check- Returns:
- true if it's absolute, false if not
- Throws:
java.io.IOException- on error
-
copyStream
public static int copyStream(java.io.InputStream is, java.io.OutputStream os) throws java.io.IOExceptionCopy an InputStream to an OutputStream. If either stream is not already buffered, then it's wrapped in the corresponding buffered stream (i.e., BufferedInputStream or BufferedOutputStream) before copying. Calling this method is equivalent to:copyStream (src, dst, 8192);
- Parameters:
is- the source InputStreamos- the destination OutputStream- Returns:
- total number of bytes copied
- Throws:
java.io.IOException- on error- See Also:
copyStream(InputStream,OutputStream,int),copyReader(Reader,Writer),copyFile(File,File)
-
copyStream
public static int copyStream(java.io.InputStream src, java.io.OutputStream dst, int bufferSize) throws java.io.IOExceptionCopy an InputStream to an OutputStream. If either stream is not already buffered, then it's wrapped in the corresponding buffered stream (i.e., BufferedInputStream or BufferedOutputStream) before copying.- Parameters:
src- the source InputStreamdst- the destination OutputStreambufferSize- the buffer size to use, or -1 for a default- Returns:
- total number of bytes copied
- Throws:
java.io.IOException- on error- See Also:
copyReader(Reader,Writer,int),copyStream(InputStream,OutputStream),copyFile(File,File)
-
copyReader
public static int copyReader(java.io.Reader reader, java.io.Writer writer, int bufferSize) throws java.io.IOExceptionCopy characters from a reader to a writer. If the reader is not already buffered, then it's wrapped in a BufferedReader, using the specified buffer size. Similarly, buffered stream (i.e., BufferedInputStream or If the writer is not already buffered, then it's wrapped in a BufferedWriter, using the specified buffer size.- Parameters:
reader- where to read fromwriter- where to write tobufferSize- buffer size to use, if reader and writer are not already buffered, or -1 to use a default size.- Returns:
- total number of characters copied
- Throws:
java.io.IOException- on error- See Also:
copyReader(Reader,Writer),copyStream(InputStream,OutputStream,int),copyStream(InputStream,OutputStream),copyFile(File,File)
-
copyReader
public static int copyReader(java.io.Reader reader, java.io.Writer writer) throws java.io.IOExceptionCopy characters from a reader to a writer, using a default buffer size.- Parameters:
reader- where to read fromwriter- where to write to- Returns:
- total number of characters copied
- Throws:
java.io.IOException- on error- See Also:
copyReader(Reader,Writer),copyStream(InputStream,OutputStream,int),copyStream(InputStream,OutputStream),copyFile(File,File)
-
copyFile
public static int copyFile(java.io.File src, java.io.File dst) throws java.io.IOExceptionCopy one file to another. This method simply copies bytes and performs no character set conversion. If you want character set conversions, usecopyTextFile(File,String,File,String).- Parameters:
src- The file to copydst- Where to copy it. Can be a directory or a file.- Returns:
- total number of bytes copied
- Throws:
java.io.IOException- on error- See Also:
copyTextFile(File,String,File,String),copyReader(Reader,Writer,int),copyReader(Reader,Writer),copyStream(InputStream,OutputStream,int),copyStream(InputStream,OutputStream)
-
copyTextFile
public static int copyTextFile(java.io.File src, java.lang.String srcEncoding, java.io.File dst, java.lang.String dstEncoding) throws java.io.IOExceptionCopy one file to another, character by character, possibly doing character set conversions- Parameters:
src- the file to copysrcEncoding- the character set encoding for the source file, or null to assume the defaultdst- Where to copy it. Can be a directory or a file.dstEncoding- the character set encoding for the destination file, or null to assume the default- Returns:
- total number of characters copied
- Throws:
java.io.IOException- on error- See Also:
copyFile(File,File),copyReader(Reader,Writer,int),copyReader(Reader,Writer),copyStream(InputStream,OutputStream,int),copyStream(InputStream,OutputStream)
-
getDefaultEncoding
public static java.lang.String getDefaultEncoding()
Get the virtual machine's default encoding.- Returns:
- the default encoding
-
getFileNameExtension
public static java.lang.String getFileNameExtension(java.io.File file)
Get the extension for a path or file name. Does not include the ".".- Parameters:
file- the file- Returns:
- the extension, or null if there isn't one
-
getFileNameExtension
public static java.lang.String getFileNameExtension(java.lang.String path)
Get the extension for a path or file name. Does not include the ".".- Parameters:
path- the file or path name- Returns:
- the extension, or null if there isn't one
-
getFileNameNoExtension
public static java.lang.String getFileNameNoExtension(java.io.File file)
Get the name of a file without its extension. Does not remove any parent directory components. Uses File.getAbsolutePath(), not File.getCanonicalPath() to get the path.- Parameters:
file- the file- Returns:
- the path without the extension
-
getFileNameNoExtension
public static java.lang.String getFileNameNoExtension(java.lang.String path)
Get the name of a file without its extension. Does not remove any parent directory components.- Parameters:
path- the path- Returns:
- the path without the extension
-
dirname
public static java.lang.String dirname(java.lang.String fileName)
Get the name of a file's parent directory. This is the directory part of the filename. For instance, "/home/foo.zip" would return "/home". This method uses the file's absolute path.- Parameters:
fileName- the file name- Returns:
- directory name part of the file's absolute pathname
- See Also:
dirname(File),basename(String)
-
dirname
public static java.lang.String dirname(java.io.File file)
Get the name of a file's parent directory. This is the directory part of the filename. For instance, "/home/foo.zip" would return "/home". This method uses the file's absolute path.- Parameters:
file- the file whose parent directory is to be returned- Returns:
- directory name part of the file's absolute pathname
- See Also:
dirname(String),basename(File)
-
basename
public static java.lang.String basename(java.lang.String fileName)
Get the base (i.e., simple file) name of a file. This is the file name stripped of any directory information. For instance, "/home/foo.zip" would return "foo.zip".- Parameters:
fileName- name of the file to get the basename for- Returns:
- file name part of the file
- See Also:
dirname(String)
-
basename
public static java.lang.String basename(java.io.File file)
Get the base (i.e., simple file) name of a file. This is the file name stripped of any directory information. For instance, "/home/foo.zip" would return "foo.zip".- Parameters:
file- the file to get the basename for- Returns:
- file name part of the file
- See Also:
basename(String),dirname(File)
-
-
DMelt 3.0 © DataMelt by jWork.ORG