📄 filelocator.java.svn-base
字号:
package com.nsi.util;
import java.io.File;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.nsi.constants.AppConstants;
/**
* @author Chris Ye, created on Oct 26, 2008
*
* FileLocator
*/
public final class FileLocator
{
private final static Log log = LogFactory.getLog(FileLocator.class);
/**
* private constructor of FileLocator
*/
private FileLocator()
{
}
private static class FileLocatorHolder
{
static FileLocator locator = new FileLocator();
}
public static FileLocator getInstance()
{
return FileLocatorHolder.locator;
}
public String searchforFile( String filename )
{
log.debug("searchforFile() -- filename["+filename+"]");
ClassLoader cl = FileLocator.class.getClassLoader();
URL url = cl.getResource( filename );
if( url != null )
{
log.debug("searchforFile() -- url["+url+"]");
return url.getFile();
}
try
{
String file = null;
url = FileLocator.class.getProtectionDomain().getCodeSource().getLocation();
log.debug("searchforFile() -- protection domain url["+url+"]");
file = url.getFile();
File dir = new File( file );
if ( ( dir.isFile() ) || ( !dir.exists() ) ) dir = dir.getParentFile();
String sourceFile = filename;
File f = null;
for ( ;( dir != null ); dir = dir.getParentFile() )
{
f = new File( dir, sourceFile );
if( f.exists() ) break;
else
{
f = new File( dir, sourceFile );
if( f.exists() ) break;
}
}
if( f.exists() )
{
return f.getAbsolutePath();
}
else
{
return null;
}
}
catch( Throwable t )
{
return null;
}
}
public String retrieveParentpath( String filename )
{
log.debug("retrieveParentpath() -- filename["+filename+"]");
String path = AppConstants.EMPTY_STRING;
try
{
File dir = new File( filename );
if ( dir.isFile()) path = dir.getParent();
}
catch( Throwable t )
{
}
return path;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -