Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 the original author or authors. See the notice.md file distributed with this work for additional information regarding copyright ownership. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.iq80.leveldb.util;
 
 
 import java.io.File;
 
 public class FileUtils
 {
     private static final int TEMP_DIR_ATTEMPTS = 10000;
 
     public static boolean isSymbolicLink(File file)
     {
         try {
             final File canonicalFile = file.getCanonicalFile();
             final File absoluteFile = file.getAbsoluteFile();
             final File parentFile = file.getParentFile();
             // a symbolic link has a different name between the canonical and absolute path
             return !canonicalFile.getName().equals(absoluteFile.getName()) ||
                     // or the canonical parent path is not the same as the file's parent path,
                     // provided the file has a parent path
                     parentFile != null && !parentFile.getCanonicalPath().equals(canonicalFile.getParent());
         }
         catch (IOException e) {
             // error on the side of caution
             return true;
         }
     }
 
     public static ImmutableList<FilelistFiles(File dir)
     {
         File[] files = dir.listFiles();
         if (files == null) {
             return ImmutableList.of();
         }
         return ImmutableList.copyOf(files);
     }
 
     public static ImmutableList<FilelistFiles(File dirFilenameFilter filter)
     {
         File[] files = dir.listFiles(filter);
         if (files == null) {
             return ImmutableList.of();
         }
         return ImmutableList.copyOf(files);
     }
 
     public static File createTempDir(String prefix)
     {
         return createTempDir(new File(System.getProperty("java.io.tmpdir")), prefix);
     }
 
     public static File createTempDir(File parentDirString prefix)
     {
         String baseName = "";
         if (prefix != null) {
             baseName += prefix + "-";
         }
 
         baseName += System.currentTimeMillis() + "-";
         for (int counter = 0; counter < counter++) {
             File tempDir = new File(parentDirbaseName + counter);
             if (tempDir.mkdir()) {
                 return tempDir;
             }
         }
         throw new IllegalStateException("Failed to create directory within "
                 +  + " attempts (tried "
                 + baseName + "0 to " + baseName + ( - 1) + ')');
     }
 
     public static boolean deleteDirectoryContents(File directory)
     {
         Preconditions.checkArgument(directory.isDirectory(), "Not a directory: %s"directory);
 
         // Don't delete symbolic link directories
         if (isSymbolicLink(directory)) {
             return false;
         }
        boolean success = true;
        for (File file : listFiles(directory)) {
            success = deleteRecursively(file) && success;
        }
        return success;
    }
    public static boolean deleteRecursively(File file)
    {
        boolean success = true;
        if (file.isDirectory()) {
            success = deleteDirectoryContents(file);
        }
        return file.delete() && success;
    }
    public static boolean copyDirectoryContents(File srcFile target)
    {
        Preconditions.checkArgument(src.isDirectory(), "Source dir is not a directory: %s"src);
        // Don't delete symbolic link directories
        if (isSymbolicLink(src)) {
            return false;
        }
        target.mkdirs();
        Preconditions.checkArgument(target.isDirectory(), "Target dir is not a directory: %s"src);
        boolean success = true;
        for (File file : listFiles(src)) {
            success = copyRecursively(filenew File(targetfile.getName())) && success;
        }
        return success;
    }
    public static boolean copyRecursively(File srcFile target)
    {
        if (src.isDirectory()) {
            return copyDirectoryContents(srctarget);
        }
        else {
            try {
                Files.copy(srctarget);
                return true;
            }
            catch (IOException e) {
                return false;
            }
        }
    }
    public static File newFile(String parentString... paths)
    {
        Preconditions.checkNotNull(parent"parent is null");
        Preconditions.checkNotNull(paths"paths is null");
        return newFile(new File(parent), ImmutableList.copyOf(paths));
    }
    public static File newFile(File parentString... paths)
    {
        Preconditions.checkNotNull(parent"parent is null");
        Preconditions.checkNotNull(paths"paths is null");
        return newFile(parent, ImmutableList.copyOf(paths));
    }
    public static File newFile(File parentIterable<Stringpaths)
    {
        Preconditions.checkNotNull(parent"parent is null");
        Preconditions.checkNotNull(paths"paths is null");
        File result = parent;
        for (String path : paths) {
            result = new File(resultpath);
        }
        return result;
    }
New to GrepCode? Check out our FAQ X