Java获取程序运行的当前工作目录

比如说,我知道一个名为f1.txt的文件和我的运行的程序在同一个目录中,那么我该怎么指向它?(不知道这个文件和我运行的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);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-01
request.getSession().getServletContext().getRealPath("/WEB-INF/classes")
获取绝对路径
剩下的就自己拼接吧
第2个回答  2011-10-01
要运行java的程序很简单啊;首先要安装java的服务器;配置运行环境就可以了追问

。。。。。。
你当我刚下载了JDK是么?
我是说,现已知一个文件在我的程序的工作目录中,如何用File指向它?
也就是问,File类中有没有获取当前程序目录的方法?
或者有什么办法能达到这样的效果?

追答

不是你是不是要获取一个文件是吗?如果是的话;用.getFileName()
我是这样写的
String fileName = System.currentTimeMillis()+"."+file.getFileExt();
String str = CommonTools.convertGbkToUtf8(file.getFileName());
file.saveAs("/upload/"+file.getFileName(), File.SAVEAS_VIRTUAL);
希望对你有帮助

追问

。。。。。。
你一直没明白我的意思。。。。。。
我的java程序运行了,但是我不知道它是在哪里运行的,我现在想知道,懂?