Topic: [原创]在类中取得当前文件所在的相对路径与绝对路径

  Print this page

1.[原创]在类中取得当前文件所在的相对路径与绝对路径 Copy to clipboard
Posted by: YuLimin
Posted on: 2005-01-19 21:22

package io;

import java.net.URL;

/**
* <p>Title: 在类中取得当前文件所在的相对路径与绝对路径</p>
*
* <p>Description: 在类中取得当前文件所在的相对路径与绝对路径</p>
*
* <p>Copyright: Copyright 1997 - 2005</p>
*
* <p>Company: Beyond DayBreak Office</p>
*
* @author YuLimin
* @version 1.0
*/
public class GetClassPath
{
public GetClassPath()
{
super();
}

/**
* main
*
* @param args String[]
*/
public static void main(String[] args)
{
GetClassPath getclasspath = new GetClassPath();
System.out.println(getclasspath.getClassPath());
}

/**
* 在类中取得当前文件所在的相对路径与绝对路径
*
* @return String
*/
public String getClassPath()
{
String strClassName = getClass().getName();
String strPackageName = "";
if(getClass().getPackage() != null)
{
strPackageName = getClass().getPackage().getName();
}
System.out.println("ClassName:" + strClassName);
System.out.println("PackageName:" + strPackageName);

String strClassFileName = "";
if(!"".equals(strPackageName))
{
strClassFileName = strClassName.substring(strPackageName.length() + 1,strClassName.length());
}
else
{
strClassFileName = strClassName;
}
System.out.println("ClassFileName:" + strClassFileName);

URL url = null;
url = getClass().getResource(strClassFileName + ".class");
String strURL = url.toString();
strURL = strURL.substring(strURL.indexOf('/') + 1,strURL.lastIndexOf('/'));
return strURL;
}
}

2.在类中取得当前文件所在的相对路径与绝对路径,解决包含有中文路径的问题! [Re: YuLimin] Copy to clipboard
Posted by: YuLimin
Posted on: 2005-01-25 08:36

package io;

import java.net.URL;

/**
* <p>Title: 在类中取得当前文件所在的相对路径与绝对路径</p>
*
* <p>Description: 在类中取得当前文件所在的相对路径与绝对路径</p>
*
* <p>Copyright: Copyright Coffee 1997 - 2005</p>
*
* <p>Company: Beyond DayBreak Office</p>
*
* @author YuLimin
* @version 1.0
*/
public class GetClassPath
{
public GetClassPath()
{
super();
}

/**
* main
*
* @param args String[]
*/
public static void main(String[] args)
{
GetClassPath getclasspath = new GetClassPath();
System.out.println(getclasspath.getClassPath());
}

/**
* 在类中取得当前文件所在的相对路径与绝对路径
*
* @return String
*/
public String getClassPath()
{
String strClassName = getClass().getName();
String strPackageName = "";
if(getClass().getPackage() != null)
{
strPackageName = getClass().getPackage().getName();
}
System.out.println("ClassName:" + strClassName);
System.out.println("PackageName:" + strPackageName);

String strClassFileName = "";
if(!"".equals(strPackageName))
{
strClassFileName = strClassName.substring(strPackageName.length() + 1,strClassName.length());
}
else
{
strClassFileName = strClassName;
}
System.out.println("ClassFileName:" + strClassFileName);

URL url = null;
url = getClass().getResource(strClassFileName + ".class");
String strURL = url.toString();
System.out.println(strURL);
try
{
strURL = java.net.URLDecoder.decode(strURL,"UTF-8");
}
catch(Exception ex)
{
ex.printStackTrace();
}
strURL = strURL.substring(strURL.indexOf('/') + 1,strURL.lastIndexOf('/'));
return strURL;
}
}

3.Re:[原创]在类中取得当前文件所在的相对路径与绝对路径 [Re: YuLimin] Copy to clipboard
Posted by: floater
Posted on: 2005-02-13 03:37

my sense is that we need to more wrapping in order to make good use of this,
because the general case is that given a class, how to know its path(relative or absolute).

I might be wrong.

4.Re:[原创]在类中取得当前文件所在的相对路径与绝对路径 [Re: YuLimin] Copy to clipboard
Posted by: cnfree
Posted on: 2005-02-18 23:55

我记得这种方法如果打包到jar文件中是不能获得正确的绝对路径的,因为我曾经试过,不知道你测试过没有


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923