hal/src/zutil/FileFinder.java

174 lines
4.5 KiB
Java
Raw Normal View History

2008-11-14 16:38:36 +00:00
package zutil;
import java.io.BufferedInputStream;
2009-01-13 00:48:46 +00:00
import java.io.BufferedReader;
2008-11-14 16:38:36 +00:00
import java.io.File;
import java.io.FileInputStream;
2009-01-13 00:48:46 +00:00
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
2008-11-14 16:38:36 +00:00
import java.net.URISyntaxException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
2008-11-14 16:38:36 +00:00
import java.util.regex.Matcher;
/**
* File path utilities
*
* @author Ziver
*/
public class FileFinder {
/**
* Returns a String with a relative path from the given path
*
* @param file is the file to get a relative path from
* @param path is the path
2008-11-14 16:38:36 +00:00
* @return A String with a relative path
*/
public static String relativePath(File file, String path){
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;
}
/**
* Returns the File object for the given file.
* Can not point to files in JAR files.
2008-11-14 16:38:36 +00:00
*
* @param path is the path to the file (no / if not absolute path)
2008-11-14 16:38:36 +00:00
* @return A File object for the file
*/
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) {
//e.printStackTrace(MultiPrintStream.out);
2008-11-14 16:38:36 +00:00
}
return null;
}
/**
* Returns the URL to the given file
*
* @param path is the path to the file (no / if not absolute path)
* @return A URL object for the file
* @throws URISyntaxException
*/
public static URL findURL(String path){
return FileFinder.class.getClassLoader().getResource(path);
}
/**
* Returns a InputStream from the path
*
* @param path is the path to the file (no / if not absolute path)
* @return A InputStream object for the file
*/
public static InputStream getInputStream(String path){
try {
File file = new File(path);
if(file!=null && file.exists()){
return new BufferedInputStream( new FileInputStream( file ) );
}
return FileFinder.class.getClassLoader().getResourceAsStream(path);
} catch (Exception e) {
//e.printStackTrace(MultiPrintStream.out);
}
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);
*
* @param file is the file to read
2009-01-13 00:48:46 +00:00
* @return The file content
* @throws IOException
*/
public static String getFileContent(File file) throws IOException{
BufferedReader in = new BufferedReader(new FileReader(file));
StringBuffer ret = new StringBuffer();
int tmp;
while((tmp=in.read()) != -1){
ret.append((char)tmp);
}
in.close();
return ret.toString();
}
2008-11-14 16:38:36 +00:00
/**
* Returns a ArrayList with all the files in a folder and sub folders
*
* @param dir is the directory to search in
2008-11-14 16:38:36 +00:00
* @return The ArrayList with the files
*/
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
*
* @param dir is the directory to search in
* @param fileList is the ArrayList to add the files to
* @param recursice is if the method should search the sub directories to.
2008-11-14 16:38:36 +00:00
* @return The ArrayList with the files
*/
public static List<File> search(File dir, List<File> fileList, boolean recursive){
2008-11-14 16:38:36 +00:00
String[] temp = dir.list();
File file;
if(temp != null){
for(int i=0; i<temp.length ;i++){
file = new File(dir.getPath()+File.separator+temp[i]);
if(recursive && file.isDirectory()){
search(new File(dir.getPath()+File.separator+temp[i]+File.separator), fileList, recursive);
}
else if(file.isFile()){
//MultiPrintStream.out.println("File Found: "+file);
fileList.add(file);
}
2008-11-14 16:38:36 +00:00
}
}
return fileList;
}
/**
* Returns the extension of the file
*
* @param file is the file
2008-11-14 16:38:36 +00:00
* @return The extension
*/
public static String fileExtension(File file){
return fileExtension(file.getName());
}
/**
* Returns the extension of the file
*
* @param file is the file
2008-11-14 16:38:36 +00:00
* @return The extension
*/
public static String fileExtension(String file){
if(file.lastIndexOf(".")==-1)
return "";
return file.substring(file.lastIndexOf(".")+1,file.length());
}
}