info.monitorenter.util
Class FileUtil

java.lang.Object
  extended by info.monitorenter.util.FileUtil

public final class FileUtil
extends java.lang.Object

Utility class for file operations.

Version:
1.1
Author:
Achim Westermann

Method Summary
static java.util.Map.Entry<java.lang.String,java.lang.String> cutDirectoryInformation(java.lang.String path)
          Cuts the path information of the String that is interpreted as a filename into the directory part and the file part.
static java.util.Map.Entry<java.lang.String,java.lang.String> cutDirectoryInformation(java.net.URL path)
          Cuts all path information of the String representation of the given URL.
static java.util.Map.Entry<java.lang.String,java.lang.String> cutExtension(java.lang.String filename)
          Cuts a String into the part before the last dot and after the last dot.
static java.io.File deriveFile(java.io.File source, java.lang.String suffix, boolean preserveExtension)
          Creates a new file by the contract of File.createTempFile(String, String, File) .
static java.lang.String formatFilesize(long filesize, java.util.Locale locale)
          Returns the formatted file size to Bytes, KB, MB or GB depending on the given value.
static java.lang.String getDefaultFileName(java.lang.String name)
          Finds a filename based on the given name.
static boolean isAllASCII(java.io.File f)
          Tests whether the given file only contains ASCII characters if interpreted by reading bytes (16 bit).
static boolean isAllASCII(java.io.InputStream in)
          Tests wether the given input stream only contains ASCII characters if interpreted by reading bytes (16 bit).
static boolean isEqual(java.io.File document, java.nio.charset.Charset a, java.nio.charset.Charset b)
          Tests, whether the content of the given file is identical at character level, when it is opened with both different Charsets.
static java.io.InputStream readCache(java.io.File f)
          Invokes readRAM(File), but decorates the result with a ByteArrayInputStream.
static byte[] readRAM(java.io.File f)
          Reads the content of the given File into an array.
static void removeDuplicateLineBreaks(java.io.File f)
          Removes the duplicate line breaks in the given file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

cutDirectoryInformation

public static java.util.Map.Entry<java.lang.String,java.lang.String> cutDirectoryInformation(java.net.URL path)
Cuts all path information of the String representation of the given URL.

 
  "file//c:/work/programming/anyfile.jar" --> "anyfile.jar"
  "http://jamwg.de"                       --> "" // No file part.
  "ftp://files.com/directory2/"           --> "" // File part of URL denotes a directory.
 
 
Assuming, that '/' is the current file separator character.

Parameters:
path - the absolute file path you want the mere file name of.
Returns:
the Map.Entry consisting of path information and file name.

cutDirectoryInformation

public static java.util.Map.Entry<java.lang.String,java.lang.String> cutDirectoryInformation(java.lang.String path)
Cuts the path information of the String that is interpreted as a filename into the directory part and the file part. The current operating system's path separator is used to cut all path information from the String.

 
  "c:/work/programming/anyfile.jar" --> Map.Entry("c:/work/programming/","anyfile.jar");
  "anyfile.jar"                     --> Map.Entry(new File(".").getAbsolutePath(),"anyfile.jar");
  "c:/directory1/directory2/"       --> Map.Entry("c:/directory1/directory2/","");
  "c:/directory1/directory2"        --> Map.Entry("c:/directory1/directory2/",""); // directory2 is a dir!
  "c:/directory1/file2"             --> Map.Entry("c:/directory1/","file2");       // file2 is a file!
  "c:/"                             --> Map.Entry("c:/","");
 
 
Assuming, that '/' is the current file separator character.

If your string is retrieved from an URL instance, use cutDirectoryInformation(URL path) instead, because URL's do not depend on the operating systems file separator!

Parameters:
path - the absolute file path you want the mere file name of.
Returns:
the Map.Entry consisting of path information and file name.

cutExtension

public static java.util.Map.Entry<java.lang.String,java.lang.String> cutExtension(java.lang.String filename)
Cuts a String into the part before the last dot and after the last dot. If only one dot is contained on the first position, it will completely be used as prefix part.

 Map.Entry entry = FileUtil.cutExtension("A.Very.Strange.Name.txt");
 String prefix = (String) entry.getKey(); // prefix is "A.Very.Strange.Name".
 String suffix = (String) entry.getValue(); // suffix is "txt";
 
 entry = FileUtil.cutExtension(".profile");
 String prefix = (String) entry.getKey(); // prefix is ".profile".
 String suffix = (String) entry.getValue(); // suffix is "";
 
 entry = FileUtil.cutExtension("bash");
 String prefix = (String) entry.getKey(); // prefix is "bash".
 String suffix = (String) entry.getValue(); // suffix is "";
 
 

Parameters:
filename - A String that is interpreted to be a file name: The last dot ('.') is interpreted to be the extension delimiter.
Returns:
A java.util.Map.Entry instance containing a String for the filename at the key side and a String for the extension at the value side.

getDefaultFileName

public static java.lang.String getDefaultFileName(java.lang.String name)
Finds a filename based on the given name. If a file with the given name does not exist, name will be returned.

Else:

  "myFile.out"     --> "myFile_0.out"
  "myFile_0.out"   --> "myFile_1.out"
  "myFile_1.out"   --> "myFile_2.out"
  ....
 

The potential extension is preserved, but a number is appended to the prefix name.

Parameters:
name - A desired file name.
Returns:
A String that sticks to the naming convention of the given String but is unique in the directory scope of argument name.

deriveFile

public static java.io.File deriveFile(java.io.File source,
                                      java.lang.String suffix,
                                      boolean preserveExtension)
                               throws java.io.IOException
Creates a new file by the contract of File.createTempFile(String, String, File) .

The resulting file will have the full path and file name without the extension concatenated with the given suffix (if preserveExtension is true also the suffix of the original file will be added to the end).

Parameters:
source - the source file to take the name of.
suffix - the suffix to append.
preserveExtension - if true the suffix of the source file will be appended to the result file.
Returns:
A file derived from the original file name.
Throws:
java.io.IOException - if something goes wrong.

isAllASCII

public static boolean isAllASCII(java.io.File f)
                          throws java.io.IOException
Tests whether the given file only contains ASCII characters if interpreted by reading bytes (16 bit).

This does not mean that the file is really an ASCII text file. It just might be viewed with an editor showing only valid ASCII characters.

Parameters:
f - the file to test.
Returns:
true if all bytes in the file are in the ASCII range.
Throws:
java.io.IOException - on a bad day.

isAllASCII

public static boolean isAllASCII(java.io.InputStream in)
                          throws java.io.IOException
Tests wether the given input stream only contains ASCII characters if interpreted by reading bytes (16 bit).

This does not mean that the underlying content is really an ASCII text file. It just might be viewed with an editor showing only valid ASCII characters.

Parameters:
in - the stream to test.
Returns:
true if all bytes in the given input stream are in the ASCII range.
Throws:
java.io.IOException - on a bad day.

isEqual

public static boolean isEqual(java.io.File document,
                              java.nio.charset.Charset a,
                              java.nio.charset.Charset b)
                       throws java.io.IOException
Tests, whether the content of the given file is identical at character level, when it is opened with both different Charsets.

This is most often the case, if the given file only contains ASCII codes but may also occur, when both codepages cover common ranges and the document only contains values m_out of those ranges (like the EUC-CN charset contains all mappings from BIG5).

Parameters:
document - the file to test.
a - the first character set to interpret the document in.
b - the 2nd character set to interpret the document in.
Returns:
true if both files have all equal contents if they are interpreted as character data in both given encodings (they may differ at binary level if both charsets are different).
Throws:
java.io.IOException - if something goes wrong.

readCache

public static java.io.InputStream readCache(java.io.File f)
                                     throws java.io.IOException
Invokes readRAM(File), but decorates the result with a ByteArrayInputStream.

This means: The complete content of the given File has been loaded before using the returned InputStream. There are no IO-delays afterwards but OutOfMemoryErrors may occur.

Parameters:
f - the file to cache.
Returns:
an input stream backed by the file read into memory.
Throws:
java.io.IOException - if something goes wrong.

readRAM

public static byte[] readRAM(java.io.File f)
                      throws java.io.IOException
Reads the content of the given File into an array.

This method currently does not check for maximum length and might cause a java.lang.OutOfMemoryError. It is only intended for performance-measurements of data-based algorithms that want to exclude I/O-usage.

Parameters:
f - the file to read.
Returns:
the contents of the given file.
Throws:
java.io.IOException - if something goes wrong.

removeDuplicateLineBreaks

public static void removeDuplicateLineBreaks(java.io.File f)
Removes the duplicate line breaks in the given file.

Be careful with big files: In order to avoid having to write a tmpfile (cannot read and directly write to the same file) a StringBuffer is used for manipulation. Big files will cost all RAM and terminate VM hard.

Parameters:
f - the file to remove duplicate line breaks in.

formatFilesize

public static java.lang.String formatFilesize(long filesize,
                                              java.util.Locale locale)
Returns the formatted file size to Bytes, KB, MB or GB depending on the given value.

Parameters:
filesize - in bytes
locale - the locale to translate the result to (e.g. in France they us
Returns:
the formatted filesize to Bytes, KB, MB or GB depending on the given value.


Copyright © 2001 - 2010 LGPL, All Rights Footloose.