2010-10-27 18:02:44 +00:00
|
|
|
package zutil.io.file;
|
2008-11-14 16:38:36 +00:00
|
|
|
|
2010-01-31 18:10:00 +00:00
|
|
|
import java.io.BufferedInputStream;
|
2008-11-14 16:38:36 +00:00
|
|
|
import java.io.File;
|
2010-01-31 18:10:00 +00:00
|
|
|
import java.io.FileInputStream;
|
2009-01-13 00:48:46 +00:00
|
|
|
import java.io.IOException;
|
2010-01-31 18:10:00 +00:00
|
|
|
import java.io.InputStream;
|
2008-11-14 16:38:36 +00:00
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.net.URL;
|
2010-07-01 16:22:02 +00:00
|
|
|
import java.util.HashMap;
|
2010-01-31 18:10:00 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
2011-06-24 23:20:59 +00:00
|
|
|
import java.util.logging.Logger;
|
2008-11-14 16:38:36 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
|
2010-10-27 18:04:52 +00:00
|
|
|
import zutil.io.IOUtil;
|
2010-10-27 18:02:44 +00:00
|
|
|
import zutil.io.MultiPrintStream;
|
2011-06-24 23:20:59 +00:00
|
|
|
import zutil.log.LogUtil;
|
2010-10-27 18:02:44 +00:00
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
/**
|
|
|
|
|
* File path utilities
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
2010-07-01 16:22:02 +00:00
|
|
|
public class FileUtil {
|
2011-06-24 23:20:59 +00:00
|
|
|
public static final Logger logger = LogUtil.getLogger();
|
2008-11-14 16:38:36 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a String with a relative path from the given path
|
|
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param file is the file to get a relative path from
|
|
|
|
|
* @param path is the path
|
|
|
|
|
* @return A String with a relative path
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public static String relativePath(File file, String path){
|
2011-06-24 23:20:59 +00:00
|
|
|
if( file == null || path == null )
|
|
|
|
|
return null;
|
2008-11-14 16:38:36 +00:00
|
|
|
String absolute = file.getAbsolutePath();
|
|
|
|
|
String tmpPath = path.replaceAll(
|
|
|
|
|
"[/\\\\]",
|
|
|
|
|
Matcher.quoteReplacement(File.separator));
|
|
|
|
|
|
|
|
|
|
String relative = absolute.substring(
|
|
|
|
|
absolute.indexOf(tmpPath)+path.length(),
|
|
|
|
|
absolute.length());
|
|
|
|
|
return relative;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-31 18:10:00 +00:00
|
|
|
* Returns the File object for the given file.
|
|
|
|
|
* Can not point to files in JAR files.
|
2008-11-14 16:38:36 +00:00
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param path is the path to the file (no / if not absolute path)
|
|
|
|
|
* @return A File object for the file
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
|
|
|
|
public static File find(String path){
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(path);
|
|
|
|
|
if(file!=null && file.exists()){
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
return new File(findURL(path).toURI());
|
|
|
|
|
} catch (Exception e) {
|
2010-07-02 20:16:37 +00:00
|
|
|
e.printStackTrace();
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 18:10:00 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the URL to the given file
|
|
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param path is the path to the file (no / if not absolute path)
|
|
|
|
|
* @return A URL object for the file
|
2010-01-31 18:10:00 +00:00
|
|
|
* @throws URISyntaxException
|
|
|
|
|
*/
|
|
|
|
|
public static URL findURL(String path){
|
2010-07-02 20:16:37 +00:00
|
|
|
return Thread.currentThread().getContextClassLoader().getResource(path);
|
2010-01-31 18:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a InputStream from the path
|
|
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param path is the path to the file (no / if not absolute path)
|
|
|
|
|
* @return A InputStream object for the file
|
2010-01-31 18:10:00 +00:00
|
|
|
*/
|
|
|
|
|
public static InputStream getInputStream(String path){
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(path);
|
|
|
|
|
if(file!=null && file.exists()){
|
|
|
|
|
return new BufferedInputStream( new FileInputStream( file ) );
|
|
|
|
|
}
|
2010-07-02 20:16:37 +00:00
|
|
|
return Thread.currentThread().getContextClassLoader()
|
|
|
|
|
.getResourceAsStream(path);
|
2010-01-31 18:10:00 +00:00
|
|
|
} catch (Exception e) {
|
2010-07-02 20:16:37 +00:00
|
|
|
e.printStackTrace();
|
2010-01-31 18:10:00 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2009-01-13 00:48:46 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads and returns the content of a file as a String.
|
|
|
|
|
* Or use FileUtils.readFileToString(file);
|
|
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param file is the file to read
|
|
|
|
|
* @return The file content
|
2009-01-13 00:48:46 +00:00
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static String getFileContent(File file) throws IOException{
|
2010-10-27 18:04:52 +00:00
|
|
|
return IOUtil.getContent( new FileInputStream(file) );
|
2010-07-01 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads and returns the content of a file as a String.
|
|
|
|
|
* Or use FileUtils.readFileToString(file);
|
|
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param url is the url to read
|
|
|
|
|
* @return The file content
|
2010-07-01 16:22:02 +00:00
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static String getContent(URL url) throws IOException{
|
2010-10-27 18:04:52 +00:00
|
|
|
return IOUtil.getContent( url.openStream() );
|
2009-01-13 00:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
2010-07-01 16:22:02 +00:00
|
|
|
/**
|
|
|
|
|
* Cache for the search functions
|
|
|
|
|
*/
|
|
|
|
|
private static HashMap<SearchItem,List<File>> search_cache = new HashMap<SearchItem,List<File>>();
|
|
|
|
|
/**
|
|
|
|
|
* An Cache Item class to identify different cached items
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
|
|
|
|
private static class SearchItem{
|
|
|
|
|
private File dir;
|
|
|
|
|
private boolean folders;
|
|
|
|
|
private int recurse;
|
|
|
|
|
|
|
|
|
|
protected SearchItem(File dir, boolean folders, int recurse){
|
|
|
|
|
this.dir = dir;
|
|
|
|
|
this.folders = folders;
|
|
|
|
|
this.recurse = recurse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean equals(Object o){
|
|
|
|
|
if(o!=null && o instanceof SearchItem){
|
|
|
|
|
SearchItem si = (SearchItem)o;
|
|
|
|
|
return dir.equals(si.dir) && folders == si.folders && recurse == si.recurse;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public int hashCode(){
|
|
|
|
|
int hash = 133;
|
|
|
|
|
hash = 23 * hash + dir.hashCode();
|
|
|
|
|
hash = 23 * hash + (folders ? 1 : 0);
|
|
|
|
|
hash = 23 * hash + recurse;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-27 18:02:44 +00:00
|
|
|
* Same as search(File dir) but it caches the result
|
|
|
|
|
* to be used next time this function is called with
|
|
|
|
|
* the same parameters.
|
2010-07-01 16:22:02 +00:00
|
|
|
*/
|
|
|
|
|
public static List<File> cachedSearch(File dir){
|
|
|
|
|
return cachedSearch(dir, new LinkedList<File>(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as search(File dir, List<File> fileList, boolean recursive)
|
2010-10-27 18:02:44 +00:00
|
|
|
* but is caches the result to be used next time this function is
|
|
|
|
|
* called with the same parameters.
|
2010-07-01 16:22:02 +00:00
|
|
|
*/
|
|
|
|
|
public static List<File> cachedSearch(File dir, List<File> fileList, boolean recursive){
|
|
|
|
|
return cachedSearch(dir, new LinkedList<File>(), false, (recursive ? Integer.MAX_VALUE : 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as search(File dir, List<File> fileList, boolean folders, int recurse)
|
|
|
|
|
* but is caches the result to be used next time this function is called
|
|
|
|
|
* with the same parameters.
|
|
|
|
|
*/
|
|
|
|
|
public static List<File> cachedSearch(File dir, List<File> fileList, boolean folders, int recurse){
|
|
|
|
|
SearchItem si = new SearchItem(dir, folders, recurse);
|
|
|
|
|
if( search_cache.containsKey(si) ){
|
|
|
|
|
fileList.addAll( search_cache.get(si) );
|
|
|
|
|
return fileList;
|
|
|
|
|
}
|
|
|
|
|
search(dir, fileList, folders, recurse);
|
|
|
|
|
search_cache.put(si, fileList);
|
|
|
|
|
return fileList;
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-14 16:38:36 +00:00
|
|
|
/**
|
2010-04-15 20:52:34 +00:00
|
|
|
* Returns a List with all the files in a folder and sub folders
|
2008-11-14 16:38:36 +00:00
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param dir is the directory to search in
|
|
|
|
|
* @return The List with the files
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
2010-01-31 18:10:00 +00:00
|
|
|
public static List<File> search(File dir){
|
|
|
|
|
return search(dir, new LinkedList<File>(), true);
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a ArrayList with all the files in a folder and sub folders
|
|
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param dir is the directory to search in
|
|
|
|
|
* @param fileList is the List to add the files to
|
|
|
|
|
* @param recursive is if the method should search the sub directories to.
|
|
|
|
|
* @return A List with the files
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
2010-01-31 18:10:00 +00:00
|
|
|
public static List<File> search(File dir, List<File> fileList, boolean recursive){
|
2010-04-15 20:52:34 +00:00
|
|
|
return search(dir, new LinkedList<File>(), false, (recursive ? Integer.MAX_VALUE : 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a ArrayList with all the files in a folder and sub folders
|
|
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param dir is the directory to search in
|
|
|
|
|
* @param fileList is the List to add the files to
|
|
|
|
|
* @param folders is if the method should add the folders to the List
|
|
|
|
|
* @param recurse is how many times it should recurse into folders
|
|
|
|
|
* @return A List with the files and/or folders
|
2010-04-15 20:52:34 +00:00
|
|
|
*/
|
|
|
|
|
public static List<File> search(File dir, List<File> fileList, boolean folders, int recurse){
|
|
|
|
|
if(recurse<0)
|
|
|
|
|
return fileList;
|
|
|
|
|
--recurse;
|
|
|
|
|
if(folders){
|
|
|
|
|
MultiPrintStream.out.println("Dir Found : "+dir);
|
|
|
|
|
fileList.add( dir );
|
|
|
|
|
}
|
2008-11-14 16:38:36 +00:00
|
|
|
|
2010-04-15 20:52:34 +00:00
|
|
|
File file;
|
|
|
|
|
String[] temp = dir.list();
|
2009-02-08 21:32:06 +00:00
|
|
|
if(temp != null){
|
|
|
|
|
for(int i=0; i<temp.length ;i++){
|
|
|
|
|
file = new File(dir.getPath()+File.separator+temp[i]);
|
2010-04-15 20:52:34 +00:00
|
|
|
if(file.isDirectory()){
|
2011-06-24 23:20:59 +00:00
|
|
|
logger.finer("Found Folder: "+file);
|
2010-04-15 20:52:34 +00:00
|
|
|
search(new File(dir.getPath()+File.separator+temp[i]+File.separator), fileList, folders, recurse);
|
2009-02-08 21:32:06 +00:00
|
|
|
}
|
|
|
|
|
else if(file.isFile()){
|
2011-06-24 23:20:59 +00:00
|
|
|
logger.finer("Found File: "+file);
|
2009-02-08 21:32:06 +00:00
|
|
|
fileList.add(file);
|
|
|
|
|
}
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the extension of the file
|
2010-01-31 18:10:00 +00:00
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param file is the file
|
|
|
|
|
* @return The extension
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
2011-02-12 19:28:13 +00:00
|
|
|
public static String getFileExtension(File file){
|
|
|
|
|
return getFileExtension(file.getName());
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the extension of the file
|
2010-01-31 18:10:00 +00:00
|
|
|
*
|
2010-10-27 18:02:44 +00:00
|
|
|
* @param file is the file
|
|
|
|
|
* @return The extension
|
2008-11-14 16:38:36 +00:00
|
|
|
*/
|
2011-02-12 19:28:13 +00:00
|
|
|
public static String getFileExtension(String file){
|
2010-12-26 18:17:35 +00:00
|
|
|
if( file == null || file.lastIndexOf(".") == -1 )
|
2008-11-14 16:38:36 +00:00
|
|
|
return "";
|
2010-12-26 18:17:35 +00:00
|
|
|
return file.substring(file.lastIndexOf(".")+1, file.length());
|
2008-11-14 16:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|