UC Berkeley Group for User Interface Research
Updated November 17, 2000

edu.berkeley.guir.lib.io
Class FileLib

java.lang.Object
  |
  +--edu.berkeley.guir.lib.io.FileLib

public class FileLib
extends Object

Miscellaneous file librariesm, for manipulating files, directories, and class files.

This software is distributed under the Berkeley Software License.

 Revisions:  - GUIRLib-v1.0-1.0.0, Jan 29 1998, JH
               Created class
             - GUIRLib-v1.2-1.0.0, Jun 22 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.3-1.0.0, Aug 11 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.4-1.0.0, Aug 31 2000, JH
               Touched for GUIRLib release
 

Since:
1.3beta
Version:
GUIRLib-v1.4-1.0.0, Aug 31 2000
Author:
Jason Hong ( jasonh@cs.berkeley.edu )

Field Summary
static String CLASS_EXT
          File extension for Java class files.
static String FILE_PROTOCOL
          HTTP file protocol header.
static String SERIALIZED_EXT
          File extension for serialized files.
static String ZIP_EXT
          File extension for zip files.
 
Method Summary
static String addFileNameExtension(File f, String strExtension)
          Ensure that the filename ends with the specified extension.
static String addFileNameExtension(String strFileName, String strExtension)
          Ensure that the filename ends with the specified extension.
static String classNameToLocalSystem(String strClassName)
          Fixes the class file name to the local system so it can be searched for in the local system.
static String fileNameToURL(String strName)
          Given a path name in the file system, convert it to a URL.
static String generateUniqueFileName()
          Generate a unique filename in the current directory.
static String generateUniqueFileName(String strRoot, String strExt)
          Generate a unique filename in the current directory.
static String generateUniqueFileName(String strPath, String strRoot, String strExt)
          Generate a unique filename in the specified directory.
static String getClassName(String strFullClassName)
          Given a fully qualified class name, extract the class name out of it.
static String getFileNameExtension(String strFileName)
          Given the name of a file, get the file extension.
static String getFilePath(String strFileName)
          Given the name of a local file, get the absolute path of that file.
static String[] getFilesInCurrentDirectory()
          Get the name of all of the files in the current working directory.
static String[] getFilesInDirectory(String strDir)
          Get the name of all of the files in the specified directory.
static String getPackageDirectory(String strFullClassName)
          Given the fully qualified name of a class, get the directory of the package.
static String getPackageName(String strFullClassName)
          Given a fully qualified class name, extract the package name out of it.
static boolean hasClassExtension(String strClassName)
          See if the String ends with the ".class" extension.
static String localNameToNetworkName(String strName)
          Convert the filename (or path) from the local system format to an http-like format.
static String localNameToZip(String strFileName)
          Fixes a file name to conform to the zip file format.
static String networkNameToLocalName(String strName)
          Convert the filename (or path) from an http-like format to the local system format.
static String stripClassExtension(String strClassName)
          Drop the ".class" extension (if it exists).
static String URLToFileName(String strName)
          Given a URL to a file in the local file system, convert it to the file name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_PROTOCOL

public static final String FILE_PROTOCOL
HTTP file protocol header.

SERIALIZED_EXT

public static final String SERIALIZED_EXT
File extension for serialized files.

ZIP_EXT

public static final String ZIP_EXT
File extension for zip files.

CLASS_EXT

public static final String CLASS_EXT
File extension for Java class files.
Method Detail

fileNameToURL

public static String fileNameToURL(String strName)
Given a path name in the file system, convert it to a URL.
Parameters:
strName - is the name of the file (or path) to convert.
Returns:
Returns the URL of this file.

URLToFileName

public static String URLToFileName(String strName)
Given a URL to a file in the local file system, convert it to the file name.
Parameters:
strName - is the URL.
Returns:
Returns the file name.

localNameToNetworkName

public static String localNameToNetworkName(String strName)
Convert the filename (or path) from the local system format to an http-like format. For example, given "c:\temp\filename.txt" in MSDOS, convert it to "c:/temp/filename.txt".
Parameters:
strName - is the name of the file (or path) to convert.
Returns:
Returns a String containing the network name of the file.

networkNameToLocalName

public static String networkNameToLocalName(String strName)
Convert the filename (or path) from an http-like format to the local system format. For example, given "/tmp/filename.txt" convert it to to "\tmp\filename.txt" on MS-DOS.
Parameters:
strName - is the name of the file (or path) to convert.
Returns:
Returns a String containing the network name of the file.

addFileNameExtension

public static String addFileNameExtension(String strFileName,
                                          String strExtension)
Ensure that the filename ends with the specified extension.
Parameters:
strFileName - is the name of the file.
strExtension - is the file extension, e.g. "gif" or "dat".
Returns:
a String that ends with strExtension.

addFileNameExtension

public static String addFileNameExtension(File f,
                                          String strExtension)
Ensure that the filename ends with the specified extension.
See Also:
addFileNameExtension(String, String)

getFileNameExtension

public static String getFileNameExtension(String strFileName)
Given the name of a file, get the file extension. Assumes that the period ('.') is used as the extension.
Parameters:
strFileName - is the name of the file.
Returns:
a String containing the extension of the file (without the period).

getFilePath

public static String getFilePath(String strFileName)
Given the name of a local file, get the absolute path of that file.
Parameters:
strFileName - is the name of a file on the local system (using local values for filename and path separators).
Returns:
Returns the local path of the file.

localNameToZip

public static String localNameToZip(String strFileName)
Fixes a file name to conform to the zip file format.
Parameters:
strFileName - is the name of the file. For example, in MS-DOS, strFileName could be "\temp\myfile.doc".
Returns:
Returns a String that has the local directory separator replaced with a '/'. For example, given "\temp\myfile.doc", it returns "/temp/myfile.doc".

generateUniqueFileName

public static String generateUniqueFileName()
Generate a unique filename in the current directory. It will be of the form "file#.tmp".
Returns:
A unique filename that does not exist in the current directory.

generateUniqueFileName

public static String generateUniqueFileName(String strRoot,
                                            String strExt)
Generate a unique filename in the current directory.
Parameters:
strRoot - is the base of the file.
strExt - is the filename extension, "" for no extension.
Returns:
A unique filename that does not exist in the current directory.

generateUniqueFileName

public static String generateUniqueFileName(String strPath,
                                            String strRoot,
                                            String strExt)
Generate a unique filename in the specified directory. It will be of the form "file#.tmp". Creates the directory if necessary.
Parameters:
strPath - is the base path of the file. Use "" for current directory. Can end with or without trailing slash, like "/etc/" or "/bin".
strRoot - is the base of the file, "" for no extension.
strExt - is the filename extension, "" for no extension. For example, ".gif" or ".exe".
Returns:
A unique filename that does not exist in the specified directory.

getFilesInCurrentDirectory

public static String[] getFilesInCurrentDirectory()
Get the name of all of the files in the current working directory. This requires security access to system property "user.dir".
Returns:
An array of Strings that contains the name of the files in the current directory.
Throws:
Throws - SecurityException if access to "user.dir" is denied.

getFilesInDirectory

public static String[] getFilesInDirectory(String strDir)
Get the name of all of the files in the specified directory.
Returns:
An array of Strings that contains the name of the files in the current directory.

stripClassExtension

public static String stripClassExtension(String strClassName)
Drop the ".class" extension (if it exists).
Parameters:
strClassName - is the name of a class (with or without package names included).
Returns:
The name of the class without the extension.

hasClassExtension

public static boolean hasClassExtension(String strClassName)
See if the String ends with the ".class" extension.
Parameters:
strClassName - is the name of a class (with or without package names included).
Returns:
true if it ends in ".class", false otherwise.

classNameToLocalSystem

public static String classNameToLocalSystem(String strClassName)
Fixes the class file name to the local system so it can be searched for in the local system.
Parameters:
strClassName - is the name of the Class to find in the form of "java.lang.Object" or "Object.class".
Returns:
Returns a String that has the '.' replaced with the local directory separator and tags ".class" on to the end. For example, "java/lang/Object.class".

getClassName

public static String getClassName(String strFullClassName)
Given a fully qualified class name, extract the class name out of it.
Parameters:
strFullClassName - is the fully qualified class name, including packages. For example, "java.lang.Object" or "java.lang.Object.class".
Returns:
Returns the class name. For example, given "java.lang.Object" or "java.lang.Object.class" it returns "Object".

getPackageName

public static String getPackageName(String strFullClassName)
Given a fully qualified class name, extract the package name out of it.
Parameters:
strFullClassName - is the fully qualified class name, including packages. For example, "java.lang.Object" or "java.lang.Object.class".
Returns:
Returns the package name. For example, given "java.lang.Object", it returns "java.lang". Given "Object", it returns an empty String "".

getPackageDirectory

public static String getPackageDirectory(String strFullClassName)
Given the fully qualified name of a class, get the directory of the package.
Parameters:
strFullClassName - is the fully qualified class name, including packages. For example, "java.lang.Object" or "java.lang.Object.class".
Returns:
Returns the package name. For example, given "java.lang.Object", it returns "java/lang/". Given "Object", it returns an empty String "".

Copyright Information