java中获得文件的物理路径

2014-11-23 22:12:31 · 作者: · 浏览: 83

  package com.bd.test;


  import java.io.File;


  import java.io.IOException;


  import java.net.URL;


  public class csDao {


  public static void main(String[] args)


  {


  csDao getclasspath = new csDao();


  System.out.println(getclasspath.getClassPath());


  File file = new File("index.html");


  try {


  System.out.println("获得webroot下文件的路径"+file.getCanonicalPath());


  } catch (IOException e) {


  // TODO Auto-generated catch block


  e.printStackTrace();


  }


  }


  /**


  *在类中取得当前文件所在的相对路径与绝对路径


  *


  * @return String


  */


  public String getClassPath()


  {


  String strClassName = getClass().getName();


  String strPackageName = "";