You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jv...@apache.org on 2001/12/05 17:04:44 UTC
cvs commit: jakarta-commons-sandbox/util/src/java/org/apache/commons/util FileUtils.java
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: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: cvs commit:
jakarta-commons-sandbox/util/src/java/org/apache/commons/util FileUtils.java
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 12/6/01 4:12 AM, "Jason van Zyl" <jv...@zenplex.com> wrote:
> On 12/6/01 4:00 AM, "Christoph Reck" <Ch...@dlr.de> wrote:
>
>> Jason,
>>
>> could you deprecate those methods in the 'rupert' FileUtils?
>
> Sure, if you like. Though I don't really think it's necessary to deprecate
> methods for classes that are in the sandbox. But I can do it if you like.
It might be a nice courtesy to note that it was taken somewhere else, but
you are right - I don't think it has to be deprecated. If someone wants to
do something with rupert, they will have to deal with the overlap if they
need to...
>
>> I guess some votes might be necessary for this...
>>
>> +1 from me (the rupert.christoph package was my contribution to find
>> a place whithin commons, Geir went forward and made that strange
>> package in the sandbox for it to be slaughtered and used :).
>>
>> :) Chirstoph
>>
>> jvanzyl@apache.org wrote:
>>>
>>> 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.
>>
>> --
>> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
>> For additional commands, e-mail: <ma...@jakarta.apache.org>
--
Geir Magnusson Jr. geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: cvs commit:
jakarta-commons-sandbox/util/src/java/org/apache/commons/util
FileUtils.java
Posted by Jason van Zyl <jv...@zenplex.com>.
On 12/6/01 4:00 AM, "Christoph Reck" <Ch...@dlr.de> wrote:
> Jason,
>
> could you deprecate those methods in the 'rupert' FileUtils?
Sure, if you like. Though I don't really think it's necessary to deprecate
methods for classes that are in the sandbox. But I can do it if you like.
> I guess some votes might be necessary for this...
>
> +1 from me (the rupert.christoph package was my contribution to find
> a place whithin commons, Geir went forward and made that strange
> package in the sandbox for it to be slaughtered and used :).
>
> :) Chirstoph
>
> jvanzyl@apache.org wrote:
>>
>> 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.
>
> --
> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
--
jvz.
Jason van Zyl
http://tambora.zenplex.org
http://jakarta.apache.org/turbine
http://jakarta.apache.org/velocity
http://jakarta.apache.org/alexandria
http://jakarta.apache.org/commons
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: cvs commit:
jakarta-commons-sandbox/util/src/java/org/apache/commons/util
FileUtils.java
Posted by Christoph Reck <Ch...@dlr.de>.
Jason,
could you deprecate those methods in the 'rupert' FileUtils?
I guess some votes might be necessary for this...
+1 from me (the rupert.christoph package was my contribution to find
a place whithin commons, Geir went forward and made that strange
package in the sandbox for it to be slaughtered and used :).
:) Chirstoph
jvanzyl@apache.org wrote:
>
> 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.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>