获取文件途径
获取文件路径
import java.io.File;
import java.io.IOException;
/**
* 获取各种路径
* @author 乐毅
*/
public class CisForUrl {
/**
* @see D:\workspace\20100823\menggov2
* @return String
*/
public static String getUrlOne(){
File file = new File("");
String path = "";
try {
path = file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
/**
* @see D:\workspace\20100823\menggov2
* @return String
*/
public static String getUrlTwo(){
String path = System.getProperty("user.dir");
return path;
}
/**
* @see /D:/workspace/20100823/menggov2/WebRoot/WEB-INF/classes/
* @return String
*/
public static String getUrlThree(){
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
return path;
}
/**
* @see /D:/workspace/20100823/menggov2/WebRoot/WEB-INF/classes/com/chinaintersoft/menggo/util/llj/
* @return String
*/
public static String getUrlFour(){
String path = CisForUrl.class.getResource("").getPath();
return path;
}
/**
* @see D:/workspace/20100823/menggov2/WebRoot
* @return String
*/
public static String getUrlFive(String className){
String path = CisForUrl.class.getResource("").getPath();
int index = path.indexOf("WEB-INF");
if(index == -1){
index = path.indexOf("bin");
}
path = path.substring(0,index);
if(path.startsWith("jar")){
// 当class文件在jar文件中时,返回"jar:file:/F:/ ..."样的路径
path = path.substring(10);
}else if(path.startsWith("file")){
// 当class文件在class文件中时,返回"file:/F:/ ..."样的路径
path = path.substring(6);
}
if(path.endsWith("/"))path = path.substring(1,path.length()-1);//不包含最后的"/"
return path;
}
public static void main(String[] args) {
System.out.println(CisForUrl.getUrlOne());
System.out.println(CisForUrl.getUrlTwo());
System.out.println(CisForUrl.getUrlThree());
System.out.println(CisForUrl.getUrlFour());
//System.out.println(CisForUrl.getUrlOne());
}
}
import java.io.File;
import java.io.IOException;
/**
* 获取各种路径
* @author 乐毅
*/
public class CisForUrl {
/**
* @see D:\workspace\20100823\menggov2
* @return String
*/
public static String getUrlOne(){
File file = new File("");
String path = "";
try {
path = file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
/**
* @see D:\workspace\20100823\menggov2
* @return String
*/
public static String getUrlTwo(){
String path = System.getProperty("user.dir");
return path;
}
/**
* @see /D:/workspace/20100823/menggov2/WebRoot/WEB-INF/classes/
* @return String
*/
public static String getUrlThree(){
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
return path;
}
/**
* @see /D:/workspace/20100823/menggov2/WebRoot/WEB-INF/classes/com/chinaintersoft/menggo/util/llj/
* @return String
*/
public static String getUrlFour(){
String path = CisForUrl.class.getResource("").getPath();
return path;
}
/**
* @see D:/workspace/20100823/menggov2/WebRoot
* @return String
*/
public static String getUrlFive(String className){
String path = CisForUrl.class.getResource("").getPath();
int index = path.indexOf("WEB-INF");
if(index == -1){
index = path.indexOf("bin");
}
path = path.substring(0,index);
if(path.startsWith("jar")){
// 当class文件在jar文件中时,返回"jar:file:/F:/ ..."样的路径
path = path.substring(10);
}else if(path.startsWith("file")){
// 当class文件在class文件中时,返回"file:/F:/ ..."样的路径
path = path.substring(6);
}
if(path.endsWith("/"))path = path.substring(1,path.length()-1);//不包含最后的"/"
return path;
}
public static void main(String[] args) {
System.out.println(CisForUrl.getUrlOne());
System.out.println(CisForUrl.getUrlTwo());
System.out.println(CisForUrl.getUrlThree());
System.out.println(CisForUrl.getUrlFour());
//System.out.println(CisForUrl.getUrlOne());
}
}