diff options
author | michelou <michelou@epfl.ch> | 2003-10-13 16:00:33 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2003-10-13 16:00:33 +0000 |
commit | 3242f383e05f2dd7197ea72ae7b2c8c7ff893a26 (patch) | |
tree | 585ded4531b766b2c06031cb2608d77f264bce34 | |
parent | 30e1c738b9dc08b90f3dace9293169fb6581dd8b (diff) | |
download | scala-3242f383e05f2dd7197ea72ae7b2c8c7ff893a26.tar.gz scala-3242f383e05f2dd7197ea72ae7b2c8c7ff893a26.tar.bz2 scala-3242f383e05f2dd7197ea72ae7b2c8c7ff893a26.zip |
- some file utility methods
-rw-r--r-- | sources/scala/tools/scalatest/FileUtils.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/sources/scala/tools/scalatest/FileUtils.java b/sources/scala/tools/scalatest/FileUtils.java new file mode 100644 index 0000000000..49a78e85fd --- /dev/null +++ b/sources/scala/tools/scalatest/FileUtils.java @@ -0,0 +1,96 @@ +/* ___ ____ ___ __ ___ _____ +** / _// __// _ | / / / _ |/_ _/ Scala test +** __\ \/ /__/ __ |/ /__/ __ | / / (c) 2003, LAMP/EPFL +** /____/\___/_/ |_/____/_/ |_//_/ +** +** $Id$ +*/ + +package scala.tools.scalatest; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; + + +class FileUtils { + + public final static String FILE_SEP = System.getProperty("file.separator"); + public final static String PATH_SEP = System.getProperty("path.separator"); + + /** + * Creates a new directory. + * If id + */ + public static boolean createDir(File dir) { + deleteDir(dir); + return dir.mkdirs(); + } + + /** + * Deletes all files and subdirectories under <code>dir</code>. + * Returns <code>true</code> if all deletions were successful. + * If a deletion fails, the method stops attempting to delete and returns false. + * (see http://javaalmanac.com/egs/java.io/DeleteDir.html) + */ + public static boolean deleteDir(File dir) { + if (dir.isDirectory()) { + String[] children = dir.list(); + for (int i = 0; i < children.length; i++) { + boolean success = deleteDir(new File(dir, children[i])); + if (!success) { + return false; + } + } + } + + // The directory is now empty so delete it + return dir.delete(); + } + + /** + * Returns the default temporary-file directory. + */ + public static File getTempDir() { + File dir = null; + try { + File dummy = File.createTempFile("dummy", null); + dir = dummy.getParentFile(); + dummy.delete(); + } catch (IOException e) {} + return dir; + } + + /** + * Compares two files using a Java implementation of the GNU diff + * available at http://www.bmsi.com/java/#diff. + * + * @param f1 + * @param f2 + * @param showDiff + */ + public static boolean compareFiles(File f1, File f2, boolean showDiff) { + boolean res = false; + try { + ByteArrayOutputStream diffStream = new ByteArrayOutputStream(); + PrintStream diffOutput = new PrintStream( + new BufferedOutputStream(diffStream), true); + System.setOut(diffOutput); + System.setErr(diffOutput); + DiffPrint.main(new String[]{ f1.getCanonicalPath(), f2.getCanonicalPath() }); + System.setOut(System.out); + System.setErr(System.err); + if (showDiff) + System.out.println(diffStream.toString()); + res = diffStream.toString().startsWith("No"); + } catch (IOException e) {} + return res; + } + + public static boolean compareFiles(File f1, File f2) { + return compareFiles(f1, f2, false); + } + +} |