commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jvan...@apache.org
Subject cvs commit: jakarta-commons-sandbox/util/src/java/org/apache/commons/util FileUtils.java
Date Wed, 05 Dec 2001 16:04:44 GMT
jvanzyl     01/12/05 08:04:43

  Modified:    util/src/java/org/apache/commons/util FileUtils.java
  Log:
  - combining some of the methods in the 'rupert' FileUtils class with
    this one.
  
  Revision  Changes    Path
  1.4       +188 -1    jakarta-commons-sandbox/util/src/java/org/apache/commons/util/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/FileUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FileUtils.java	2001/10/08 19:38:50	1.3
  +++ FileUtils.java	2001/12/05 16:04:43	1.4
  @@ -54,11 +54,16 @@
    * <http://www.apache.org/>.
    */
   
  +import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +
   /**
    * Common {@link java.io.File} manipulation routines.
    *
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  - * @version $Id: FileUtils.java,v 1.3 2001/10/08 19:38:50 dlr Exp $
  + * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph.Reck</a>
  + * @version $Id: FileUtils.java,v 1.4 2001/12/05 16:04:43 jvanzyl Exp $
    */
   public class FileUtils
   {
  @@ -106,5 +111,187 @@
           }
   
           return displaySize;
  +    }
  +
  +    /**
  +     * Returns the directory path portion of a file specification string.
  +     * Matches the equally named unix command.
  +     * @return The directory portion excluding the ending file separator.
  +     */
  +    public static String dirname(String filename)
  +    {
  +        int i = filename.lastIndexOf(File.separator);
  +        return (i >= 0) ? filename.substring(0, i) : "";
  +    }
  +
  +    /**
  +     * Returns the filename portion of a file specification string.
  +     * @return The filename string with extension.
  +     */
  +    public static String filename(String filename)
  +    {
  +        int i = filename.lastIndexOf(File.separator);
  +        return (i >= 0) ? filename.substring(i + 1) : filename;
  +    }
  +
  +    /**
  +     * Returns the filename portion of a file specification string.
  +     * Matches the equally named unix command.
  +     * @return The filename string without extension.
  +     */
  +    public static String basename(String filename)
  +    {
  +        return basename(filename, extension(filename));
  +    }
  +
  +    /**
  +     * Returns the filename portion of a file specification string.
  +     * Matches the equally named unix command.
  +     */
  +    public static String basename(String filename, String suffix)
  +    {
  +        int i = filename.lastIndexOf(File.separator) + 1;
  +        int lastDot = ((suffix != null) && (suffix.length() > 0))
  +          ? filename.lastIndexOf(suffix) : -1;
  +
  +        if (lastDot >= 0)
  +        {
  +          return filename.substring(i, lastDot);
  +        }          
  +        else if (i > 0)
  +        {
  +          return filename.substring(i);
  +        }          
  +        else
  +        {
  +          return filename; // else returns all (no path and no extension)
  +        }          
  +    }
  +
  +    /**
  +     * Returns the extension portion of a file specification string.
  +     * This everything after the last dot '.' in the filename (including
  +     * the dot).
  +     */
  +    public static String extension(String filename)
  +    {
  +        int lastDot = filename.lastIndexOf('.');
  +
  +        if (lastDot >= 0)
  +        {
  +          return filename.substring(lastDot);
  +        }          
  +        else
  +        {
  +          return "";
  +        }          
  +    }
  +
  +    /**
  +     * Check if a file exits.
  +     *
  +     * @param fileName The name of the file to check.
  +     * @return true if file exists.
  +     */
  +    public static boolean fileExists(String fileName)
  +    {
  +        File file = new File(fileName);
  +        return file.exists();
  +    }
  +
  +    /**
  +     * Reads the contents of a file.
  +     *
  +     * @param fileName The name of the file to read.
  +     * @return The file contents or null if read failed.
  +     */
  +    public static String fileRead(String fileName) throws Exception
  +    {
  +        StringBuffer buf = new StringBuffer();
  +
  +        FileInputStream in = new FileInputStream(fileName);
  +
  +        int count;
  +        byte[] b = new byte[512];
  +        while ( (count = in.read(b)) > 0 )  // blocking read
  +        {
  +            buf.append( new String(b, 0, count) );
  +        }            
  +
  +        in.close();
  +
  +        return buf.toString();
  +    }
  +
  +    /**
  +     * Writes data to a file. The file will be created if it does not exist.
  +     *
  +     * @param fileName The name of the file to write.
  +     * @param data The content to write to the file.
  +     */
  +    public static void fileWrite(String fileName, String data) throws Exception
  +    {
  +        FileOutputStream out = new FileOutputStream(fileName);
  +        out.write( data.getBytes() );
  +        out.close();
  +    }
  +
  +    /**
  +     * Deletes a file.
  +     *
  +     * @param fileName The name of the file to delete.
  +     */
  +    public static void fileDelete(String fileName)
  +    {
  +        File file = new File(fileName);
  +        file.delete();
  +    }
  +
  +    /**
  +     * Waits for NFS to propagate a file creation, imposing a timeout.
  +     *
  +     * @param fileName The name of the file.
  +     * @param seconds The maximum time in seconds to wait.
  +     * @return True if file exists.
  +     */
  +    public static boolean waitFor(String fileName, int seconds)
  +    {
  +        File file = new File(fileName);
  +        int timeout = 0;
  +        int tick = 0;
  +        while ( ! file.exists() )
  +        {
  +            if (tick++ >= 10)
  +            {
  +                tick = 0;
  +                if (timeout++ > seconds)
  +                {
  +                    return false;
  +                }                    
  +            }
  +            try
  +            {
  +                Thread.sleep(100);
  +            }
  +            catch(InterruptedException ignore)
  +            {
  +            }
  +            catch(Exception ex)
  +            {
  +                break;
  +            }
  +        }
  +        return true;
  +    }
  +
  +    /**
  +     * Creates a file handle.
  +     *
  +     * @param fileName The name of the file.
  +     * @return A <code>File</code> instance.
  +     */
  +    public static File getFile(String fileName)
  +    {
  +        return new File(fileName);
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message