Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
jerry - Common Java Functionality Copyright (c) 2012-2015, Sandeep Gupta http://sangupta.com/projects/jerry 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 com.sangupta.jerry.util;
 
 import java.io.File;
 import java.util.List;
 
 
Utility functions around common file operations.

Author(s):
sangupta
 
 public class FileUtils {

Return a java.io.File object for the user's home directory.

Returns:
 
 	public static File getUsersHomeDirectory() {
 	}

Return a java.io.File object for the user's working directory.

Returns:
 
 	public static File getUsersWorkingDirectory() {
 	}

Returns if the given file path is an absolute file path or not.

Parameters:
filePath the file path to search for
Returns:
true if the filepath is absolute, false otherwise
 
 	public static boolean isAbsolutePath(String filePath) {
 		if(filePath == null) {
 			throw new IllegalArgumentException("Filepath cannot be null");
 		}
 		
 		// normalize
 		filePath = FilenameUtils.normalize(filePath);
 		
 		// now check
 		String prefix = FilenameUtils.getPrefix(filePath);
 		
 		if(AssertUtils.isEmpty(prefix)) {
 			return false;
 		}
 		
 		return true;
 	}

List all files in the given path, assuming the current directory to be . and without recursive scanning.

Parameters:
filePathWithWildCards the path to scan for
Returns:
collection of matched files
	public static List<FilelistFiles(String filePathWithWildCards) {
		return listFiles(new File("."), filePathWithWildCardsfalse);
	}

List all files in the given path, assuming the current directory to be . and without recursive scanning.

Parameters:
filePathWithWildCards the path to scan for
recursive whether to recurse into sub-folders or not
Returns:
collection of matched files
	public static List<FilelistFiles(String filePathWithWildCardsboolean recursive) {
		return listFiles(new File("."), filePathWithWildCardsrecursive);
	}

List all files in the given path.

Parameters:
baseDir the base directory to use
filePathWithWildCards the file path to scan for
recursive whether to recurse into sub-folders or not
Returns:
collection of matched files
	public static List<FilelistFiles(File baseDirString filePathWithWildCardsboolean recursive) {
		return listFiles(baseDirfilePathWithWildCardsrecursive, (List<IOFileFilter>) null);
	}

List all files in the given path.

Parameters:
baseDir the base directory to use
filePathWithWildCards the file path to scan for
recursive whether to recurse into sub-folders or not
additionalFilters additional org.apache.commons.io.filefilter.IOFileFilters to use during scan
Returns:
collection of matched files
	public static List<FilelistFiles(File baseDirString filePathWithWildCardsboolean recursiveIOFileFilter[] additionalFilters) {
		final List<IOFileFilterlist = new ArrayList<IOFileFilter>();
		if(AssertUtils.isNotEmpty(additionalFilters)) {
			list.addAll(Arrays.asList(additionalFilters));
		}
		return listFiles(baseDirfilePathWithWildCardsrecursivelist);
	}

List the files in the given path string with wild cards.

Parameters:
baseDir the base directory to search for files in
filePathWithWildCards the file path to search in - can be absolute
recursive whether to look in sub-directories or not
additionalFilters additional file filters that need to be used when scanning for files
Returns:
the list of files and directories that match the criteria
	public static List<FilelistFiles(File baseDirString filePathWithWildCardsboolean recursiveList<IOFileFilteradditionalFilters) {
		if(filePathWithWildCards == null) {
			throw new IllegalArgumentException("Filepath cannot be null");
		}
		// change *.* to *
		filePathWithWildCards = filePathWithWildCards.replace("*.*""*");
		// normalize
		filePathWithWildCards = FilenameUtils.normalize(filePathWithWildCards);
		if(filePathWithWildCards.endsWith(.)) {
			filePathWithWildCards += "*";
		}
		// check if this is an absolute path or not
		String prefix = FilenameUtils.getPrefix(filePathWithWildCards);
		final boolean isAbsolute = !prefix.isEmpty();
		// change the base dir if absolute directory
		if(isAbsolute) {
			baseDir = new File(filePathWithWildCards);
			if(!baseDir.isDirectory()) {
				// not a directory - get the base directory
				filePathWithWildCards = baseDir.getName();
				if(filePathWithWildCards.equals("~")) {
					filePathWithWildCards = "*";
				}
				if(AssertUtils.isEmpty(filePathWithWildCards)) {
					filePathWithWildCards = "*";
				}
				baseDir = baseDir.getParentFile();
				if(baseDir == null) {
					baseDir = getUsersHomeDirectory();
				}
			}
		}
		// check if the provided argument is a directory
		File dir = new File(filePathWithWildCards);
		if(dir.isDirectory()) {
			baseDir = dir.getAbsoluteFile();
			filePathWithWildCards = "*";
else {
			// let's check for base directory
			File parent = dir.getParentFile();
			if(parent != null) {
				baseDir = parent.getAbsoluteFile();
				filePathWithWildCards = dir.getName();
			}
		}
		// check for user home
		String basePath = baseDir.getPath();
		if(basePath.startsWith("~")) {
			basePath = getUsersHomeDirectory().getAbsolutePath() + . + basePath.substring(1);
			basePath = FilenameUtils.normalize(basePath);
			baseDir = new File(basePath);
		}
		// now read the files
		WildcardFileFilter wildcardFileFilter = new WildcardFileFilter(filePathWithWildCards.);
		IOFileFilter finalFilter = wildcardFileFilter;
		if(AssertUtils.isNotEmpty(additionalFilters)) {
			additionalFilters.add(0, wildcardFileFilter);
			finalFilter = new AndFileFilter(additionalFilters);
		}
		Collection<Filefiles = org.apache.commons.io.FileUtils.listFiles(baseDirfinalFilterrecursive ? . : .);
		return (List<File>) files;
	}

Find the extension of the file. If the file has no extension, an empty string is returned. If the file instance is null, a null is returned back.

Parameters:
file file for which extension is needed
Returns:
	public static String getExtension(File file) {
		if(file == null) {
			return null;
		}
		String name = file.getName();
		int index = name.lastIndexOf('.');
		if(index == -1) {
		}
		return name.substring(index + 1);
	}

Return the MD5 value for the file.

Parameters:
file
Returns:
	public static byte[] getMD5(File file) {
		final MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("MD5");
			return null;
		}
		return getFileDigestValue(filedigest);
	}

Return the SHA-256 value for the file.

Parameters:
file
Returns:
	public static byte[] getSHA256(File file) {
		final MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("SHA-256");
			return null;
		}
		return getFileDigestValue(filedigest);
	}

Run a java.security.MessageDigest algorithm over the contents of the entire file

Parameters:
file
digest
Returns:
null if file is null, cannot be read, or digest is null. Otherwise returns the actual digest value
	public static byte[] getFileDigestValue(File filefinal MessageDigest digest) {
		if(digest == null) {
			return null;
		}
		GenericConsumer<byte[]> byteConsumer = new GenericConsumer<byte[]>() {
			public boolean consume(byte[] data) {
				digest.update(data);
				return true;
			}
		};
		new FileByteChunkConsumer(filebyteConsumer).consume();
		return digest.digest();
	}
New to GrepCode? Check out our FAQ X