javalinux获取路径怎么写

1. java在linux下操作文件路径怎么写 一般文件路径在windows中用 \ 表示,但是在其他系统平台下比如linux中就不是 \ 所以java给我们提供了一个与平台无关的表示路径的常量 File.separator在windows中则表示 \ 比如现在有一个文件在D:\java\src\myjava中,如何用绝对路径访问呢? 现在建立一个目录: File fDir=new File(File.separator); //File.separator表示根目录,比如现在就表示在D盘下 。
String strFile="java"+File.separator+"src"+File.separator+"myjava"; //这个就是绝对路径 File f=new File(fDir,strFile); 。
2. java在linux下操作文件路径怎么写 一般文件路径在windows中用 \ 表示,但是在其他系统平台下比如linux中就不是 \ 所以java给我们提供了一个与平台无关的表示路径的常量 File.separator在windows中则表示 \ 比如现在有一个文件在D:\java\src\myjava中,如何用绝对路径访问呢?
现在建立一个目录:
File fDir=new File(File.separator); //File.separator表示根目录,比如现在就表示在D盘下 。
String strFile="java"+File.separator+"src"+File.separator+"myjava"; //这个就是绝对路径
File f=new File(fDir,strFile);
3. linux下 Java如何获取文件的绝对路径 需要使用路径时,用下面的方法取得项目根目录的绝对路径(Tools为方法类)
public static String getRootPath() {
String classPath = Tools.class.getClassLoader().getResource("/").getPath();
String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
return rootPath;
}
4. java如何获得linux下web路径 java获取根路径有两种方式:
1),在servlet可以用一下方法取得:
request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”) ”//upload//”;
2),不从jsp,或servlet中获取,只从普通java类中获取:
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
SAXReader() saxReader = new SAXReader();
if(path.indexOf(“WEB-INF”)>0){
【javalinux获取路径怎么写】path = path.substring(0,path.indexOf(“/WEB-INF/classes”) 16);
// '/WEB-INF/classes'为16位
document = saxReader.read(path filename);
}else{
document = saxReader.read(getClass().getResourceAsStream(filename));
}
weblogic tomcat 下都有效
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();