1、利用System.getProperty()函数获取当前路径:
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
2、使用File提供的函数获取当前路径:
File directory = new File("");//设定为当前文件夹
try{
System.out.println(directory.getCanonicalPath());//获取标准的路径
System.out.println(directory.getAbsolutePath());//获取绝对路径
}catch(Exceptin e){}
File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new
File("..")两种路径有所区别。
# 对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
# 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径
# 至于getPath()函数,得到的只是你在new File()时设定的路径
Java基础知识教程:
⑵ 请问JAVA怎么获取本地计算机的目录啊
直接抄给你发代码呢!!O(∩_∩)O~
package ;
import java.io.File;
public class uu {
public static void main(String[] args){
File[] roots = File.listRoots();
for (int i=0; i<roots.length; i++){
System.out.println(roots[i]);
}
⑶ 通过java获取当前项目路径
getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)
例如 项目在/D:/workspace/MainStream/Test
在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/
publicStringgetCurrentPath(){
//取得根目录路径
StringrootPath=getClass().getResource("/").getFile().toString();
//当前目录路径
StringcurrentPath1=getClass().getResource(".").getFile().toString();
StringcurrentPath2=getClass().getResource("").getFile().toString();
//当前目录的上级目录路径
StringparentPath=getClass().getResource("../").getFile().toString();
returnrootPath;
}
⑷ Java获取程序运行的当前工作目录
使用下面这个PathUtil的getProgramPath()就可以获得当前程序运行的目录。
import java.net.URL;
import java.net.URLDecoder;
class PathUtil {
/**
* Get the env of windir, such as "C:\WINDOWS".
*
* @return the env of windir value.
*/
public static String getWindir() {
return System.getenv("windir");
}
/**
* Get file separator, such as "/" on unix.
*
* @return the separator of file.
*/
public static String getFileSeparator() {
return System.getProperty("file.separator");
}
/**
* Get line separator, such as "\n" on unix.
*
* @return the separator of line.
*/
public static String getLineSeparator() {
return System.getProperty("line.separator");
}
/**
* Get programPath
*
* @return programPath
*/
public static String getProgramPath() {
Class<PathUtil> cls = PathUtil.class;
ClassLoader loader = cls.getClassLoader();
//
// Get the full name of the class.
//
String clsName = cls.getName() + ".class";
//
// Get the package that include the class.
//
Package pack = cls.getPackage();
String path = "";
//
// Transform package name to path.
//
if (pack != null) {
String packName = pack.getName();
//
// Get the class's file name.
//
clsName = clsName.substring(packName.length() + 1);
//
// If package is simple transform package name to path directly,
// else transform package name to path by package name's
// constituent.
//
path = packName;
if (path.indexOf(".") > 0) {
path = path.replace(".", "/");
}
path = path + "/";
}
URL url = loader.getResource(path + clsName);
//
// Get path information form the instance of URL.
//
String retPath = url.getPath();
//
// Delete protocol name "file:" form path information.
//
try {
int pos = retPath.indexOf("file:");
if (pos > -1) {
retPath = retPath.substring(pos + 5);
}
//
// Delete the information of class file from the information of
// path.
//
pos = retPath.indexOf(path + clsName);
retPath = retPath.substring(0, pos - 1);
//
// If the class file was packageed into JAR e.g. file, delete the
// file name of the corresponding JAR e.g..
//
if (retPath.endsWith("!")) {
retPath = retPath.substring(0, retPath.lastIndexOf("/"));
}
retPath = URLDecoder.decode(retPath, "utf-8");
} catch (Exception e) {
retPath = null;
e.printStackTrace();
}
return retPath;
}
}
测试类:
public class Test{
public static void main(String args[]){
String s = PathUtil.getProgramPath();
System.out.println(s);
}
}
⑸ 在java类中怎么获得java项目的目录
一 相对路径的获得
说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的项目还是web项目)
String relativelyPath=System.getProperty("user.dir");
上述相对路径中,java项目中的文件是相对于项目的根目录
web项目中的文件路径视不同的web服务器不同而不同(tomcat是相对于 tomcat安装目录\bin)
二 类加载目录的获得(即当运行时某一类时获得其装载目录)
1.1)通用的方法一(不论是一般的java项目还是web项目,先定位到能看到包路径的第一级目录)
InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
(test.txt文件的路径为 项目名\src\test.txt;类TestAction所在包的第一级目录位于src目录下)
上式中将TestAction,test.txt替换成对应成相应的类名和文件名字即可
1.2)通用方法二 (此方法和1.1中的方法类似,不同的是此方法必须以'/'开头,参考http://riddickbryant.iteye.com/blog/436693)
InputStream is=Test1.class.getResourceAsStream("/test.txt");
(test.txt文件的路径为 项目名\src\test.txt,类Test1所在包的第一级目录位于src目录下)
三 web项目根目录的获得(发布之后)
1 从servlet出发
可建立一个servlet在其的init方法中写入如下语句
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (关键)
结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\ (002_ext为项目名字)
如果是调用了s1.getRealPath("")则输出D:\工具\Tomcat-6.0\webapps\002_ext(少了一个"\")
2 从httpServletRequest出发
String cp11111=request.getSession().getServletContext().getRealPath("/");
结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\
四 classpath的获取(在Eclipse中为获得src或者classes目录的路径)
方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()
eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("t---"+t);
输出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/
方法二 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse为src某一个包中的类,下同)
eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);
输出: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/
另外,如果想把文件放在某一包中,则可以 通过以下方式获得到文件(先定位到该包的最后一级目录)
eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);
输出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse为src目录下jdom包中的类)
四 属性文件的读取:
方法 一
InputStream in = lnew BufferedInputStream( new FileInputStream(name)); Properties p = new Properties(); p.load(in);
注意路径的问题,做执行之后就可以调用p.getProperty("name")得到对应属性的值
方法二
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest", locale);
String value = localResource.getString("test");
System.out.println("ResourceBundle: " + value);
工程src目录下propertiesTest.properties(名字后缀必须为properties)文件内容如下:
test=hello word
⑹ java中如何获得运行中的.class文件的存放目录的绝对路径
类名.class.getResource("");
java.lang.Class.getResource()查找给定名字的资源
importjava.net.URL;importjava.lang.*;publicclassClassDemo{
publicstaticvoidmain(String[]args)throwsException{
ClassDemoc=newClassDemo();
Classcls=c.getClass();
//
URLurl=cls.getResource("file.txt");
System.out.println("Value="+url);
//
url=cls.getResource("newfolder/a.txt");
System.out.println("Value="+url);
}}
结果:
Value=file:/C:/Program%20Files/Java/jdk1.6.0_06/bin/file.txt
Value=null
⑺ 如何查找java路径
1、要解决问题之前,我们需要下载java这个软件,在浏览器上搜索,记住下载的具体位置,版方便下一步的操作。
⑻ 如何查看java读取文件的路径
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。
下面是上面程序在我电脑上的输出:
G:\xhuoj\konw\.\src\G:\xhuoj\konw\src\
⑼ Java 获取路径的几种方法
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);结果:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
获取当前类的绝对路径;第二种:File directory = new File("");//参数为空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);结果:C:\Documents and Settings\Administrator\workspace\projectName
获取当前类的所在工程路径;第三种:URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);结果:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
获取当前工程src目录下selected.txt文件的路径第四种:System.out.println(System.getProperty("user.dir"));结果:C:\Documents and Settings\Administrator\workspace\projectName
获取当前工程路径第五种:System.out.println( System.getProperty("java.class.path"));结果:C:\Documents and Settings\Administrator\workspace\projectName\bin获取当前工程路径