Repository: jena Updated Branches: refs/heads/master 75087f52e -> 7fa09e8a9
Add "clearAll" (use with care!) Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/f4ee4cae Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/f4ee4cae Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/f4ee4cae Branch: refs/heads/master Commit: f4ee4caecbaef6740b97182cbe3e43cbf864e152 Parents: 75087f5 Author: Andy Seaborne <a...@apache.org> Authored: Tue Nov 4 12:11:29 2014 +0000 Committer: Andy Seaborne <a...@apache.org> Committed: Tue Nov 4 12:11:29 2014 +0000 ---------------------------------------------------------------------- .../java/org/apache/jena/atlas/lib/FileOps.java | 228 ++++++++++--------- 1 file changed, 120 insertions(+), 108 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/f4ee4cae/jena-arq/src/main/java/org/apache/jena/atlas/lib/FileOps.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/main/java/org/apache/jena/atlas/lib/FileOps.java b/jena-arq/src/main/java/org/apache/jena/atlas/lib/FileOps.java index afae19d..98a8616 100644 --- a/jena-arq/src/main/java/org/apache/jena/atlas/lib/FileOps.java +++ b/jena-arq/src/main/java/org/apache/jena/atlas/lib/FileOps.java @@ -16,7 +16,7 @@ * limitations under the License. */ -package org.apache.jena.atlas.lib; +package org.apache.jena.atlas.lib ; import java.io.File ; import java.io.FileInputStream ; @@ -28,55 +28,64 @@ import org.apache.jena.atlas.AtlasException ; import org.apache.jena.atlas.io.IO ; import org.apache.jena.atlas.logging.Log ; -public class FileOps -{ +/** A library of utility operations on files and the filing system */ +public class FileOps { + // Update implementations as standard library functions appear that meet the contract. private FileOps() {} - - /** Delete a file + + /** + * Delete a file * * @param filename */ - public static void delete(String filename) - { + public static void delete(String filename) { delete(new File(filename), true) ; } - + /* Delete a file - don't check it worked */ - - public static void deleteSilent(String filename) - { + + public static void deleteSilent(String filename) { delete(new File(filename), false) ; } - - public static void delete(File f, boolean reportExistsAfter) - { + + public static void delete(File f, boolean reportExistsAfter) { try { - /* Note: On windows, deleting a file which has been memory - * mapped does not delete the file. - */ + /* + * Note: On windows, deleting a file which has been memory mapped + * does not delete the file. + */ f.delete() ; if ( reportExistsAfter && f.exists() ) - Log.warn(FileOps.class, "delete: *** File still exists: "+f) ; - } catch (SecurityException ex) - { - Log.warn(FileOps.class, "delete: "+f+": Security exception; "+ex.getMessage()) ; + Log.warn(FileOps.class, "delete: *** File still exists: " + f) ; + } + catch (SecurityException ex) { + Log.warn(FileOps.class, "delete: " + f + ": Security exception; " + ex.getMessage()) ; } - + } - - public static void clearDirectory(String dir) - { + + /** Delete all files in a directory */ + public static void clearDirectory(String dir) { File d = new File(dir) ; - for ( File f : d.listFiles()) - { + for ( File f : d.listFiles() ) { if ( f.isFile() ) delete(f, false) ; } } - /** See if there are any files in this directory */ - public static boolean existsAnyFiles(String dir) - { + /** Delete all files and directories (recursively) in a directory */ + public static void clearAll(File d) { + for ( File f : d.listFiles() ) { + if ( ".".equals(f.getName()) || "..".equals(f.getName()) ) + continue ; + if ( f.isDirectory() ) + clearAll(f) ; + f.delete() ; + } + } + + /** See if there are any files in this directory */ + public static boolean existsAnyFiles(String dir) { File d = new File(dir) ; File[] entries = d.listFiles() ; if ( entries == null ) @@ -85,135 +94,138 @@ public class FileOps return entries.length > 0 ; } - public static boolean exists(String path) - { + /** Test for existence */ + public static boolean exists(String path) { File f = new File(path) ; - return f.exists() ; + return f.exists() ; } - - public static boolean isEmpty(String filename) - { + + /** Test for an empty file */ + public static boolean isEmpty(String filename) { File f = new File(filename) ; - if ( f.exists() ) return true ; - if ( f.isFile() ) return f.length() == 0 ; + if ( f.exists() ) + return true ; + if ( f.isFile() ) + return f.length() == 0 ; throw new AtlasException("Not a file") ; } - public static void ensureDir(String dirname) - { + /** Ensure a directory exists */ + public static void ensureDir(String dirname) { File dir = new File(dirname) ; - if ( ! dir.exists() ) + if ( !dir.exists() ) dir.mkdirs() ; } - - /** Split a file name into path, basename and extension. Nulls returned if don't make sense. */ - public static Tuple<String> splitDirBaseExt(String filename) - { + + /** + * Split a file name into path, basename and extension. Nulls returned if + * don't make sense. + */ + public static Tuple<String> splitDirBaseExt(String filename) { String path = null ; String basename = filename ; String ext = null ; - + int j = filename.lastIndexOf('/') ; if ( j < 0 ) j = filename.lastIndexOf('\\') ; - if ( j >= 0 ) - { + if ( j >= 0 ) { path = filename.substring(0, j) ; - basename = filename.substring(j+1) ; + basename = filename.substring(j + 1) ; } - + int i = basename.lastIndexOf('.') ; - - if ( i > -1 ) - { - ext = basename.substring(i+1) ; + + if ( i > -1 ) { + ext = basename.substring(i + 1) ; basename = basename.substring(0, i) ; } - + return Tuple.createTuple(path, basename, ext) ; } - - /** Split a file name into path and filename. Nulls returned if don't make sense. */ - public static Tuple<String> splitDirFile(String filename) - { + + /** + * Split a file name into path and filename. Nulls returned if don't make + * sense. + */ + public static Tuple<String> splitDirFile(String filename) { String path = null ; String fn = filename ; - + int j = filename.lastIndexOf('/') ; if ( j < 0 ) j = filename.lastIndexOf('\\') ; - - if ( j >= 0 ) - { + + if ( j >= 0 ) { path = filename.substring(0, j) ; - fn = filename.substring(j+1) ; + fn = filename.substring(j + 1) ; } return Tuple.createTuple(path, fn) ; } /** Return the basename (no path, no extension) */ - public static String basename(String filename) - { + public static String basename(String filename) { int j = filename.lastIndexOf('/') ; if ( j < 0 ) j = filename.lastIndexOf('\\') ; - String fn = ( j >= 0 ) ? filename.substring(j+1) : filename ; + String fn = (j >= 0) ? filename.substring(j + 1) : filename ; int i = fn.lastIndexOf('.') ; - + if ( i > -1 ) return fn.substring(0, i) ; return fn ; } - + /** Return the extension (or "") */ public static String extension(String filename) { - int iSlash = filename.lastIndexOf( '/' ); - int iBack = filename.lastIndexOf( '\\' ); - int iExt = filename.lastIndexOf( '.' ); - if (iBack > iSlash) iSlash = iBack; - return iExt > iSlash ? filename.substring( iExt+1 ).toLowerCase() : ""; - } - - public static String fullPath(String filename) - { - File f = new File(filename); - return f.getAbsolutePath(); - } - - public static String fullDirectoryPath(String filename) - { - File f = new File(filename); - if (f.isDirectory()) { - return f.getAbsolutePath(); - } else if (f.getParentFile() != null) { - return f.getParentFile().getAbsolutePath(); + int iSlash = filename.lastIndexOf('/') ; + int iBack = filename.lastIndexOf('\\') ; + int iExt = filename.lastIndexOf('.') ; + if ( iBack > iSlash ) + iSlash = iBack ; + return iExt > iSlash ? filename.substring(iExt + 1).toLowerCase() : "" ; + } + + public static String fullPath(String filename) { + File f = new File(filename) ; + return f.getAbsolutePath() ; + } + + public static String fullDirectoryPath(String filename) { + File f = new File(filename) ; + if ( f.isDirectory() ) { + return f.getAbsolutePath() ; + } else if ( f.getParentFile() != null ) { + return f.getParentFile().getAbsolutePath() ; } else { - return f.getAbsolutePath(); + return f.getAbsolutePath() ; } } - + /** Copy a file */ public static void copyFile(File source, File dest) { try { @SuppressWarnings("resource") - FileChannel sourceChannel = new FileInputStream(source).getChannel(); + FileChannel sourceChannel = new FileInputStream(source).getChannel() ; @SuppressWarnings("resource") - FileChannel destChannel = new FileOutputStream(dest).getChannel(); - destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); - sourceChannel.close(); - destChannel.close(); - } catch (IOException ex) { IO.exception(ex); } - } - - -// public static String getExt(String filename) -// { -// int i = filename.lastIndexOf('.') ; -// int j = filename.lastIndexOf('/') ; -// if ( i > j ) -// return filename.substring(i+1) ; -// return null ; -// } + FileChannel destChannel = new FileOutputStream(dest).getChannel() ; + destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()) ; + sourceChannel.close() ; + destChannel.close() ; + } + catch (IOException ex) { + IO.exception(ex) ; + } + } + + // public static String getExt(String filename) + // { + // int i = filename.lastIndexOf('.') ; + // int j = filename.lastIndexOf('/') ; + // if ( i > j ) + // return filename.substring(i+1) ; + // return null ; + // } }