java 如何获取一个文件的绝对路径。。。

hongseyouyang 2010-09-09 11:36:19
例如
InputStream inStream=new FileInputStream("a/b.txt");

怎么能得到a/b.txt的绝对路径,b.txt在我的workspace下。

...全文
34542 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongseyouyang 2010-09-09
  • 打赏
  • 举报
回复
例如:
D:/workspace/projectName/WebContent/dat/mm.txt

现在我把mm.txt文件放到WebContent/dat/下了,

如何能取得绝对路径。。?
阿_布 2010-09-09
  • 打赏
  • 举报
回复
A.getAbsolutePath()就可以了
arlen 2010-09-09
  • 打赏
  • 举报
回复

File A=new File("文件名及其路径");
String path = A.getCanonicalPath();
BufferedReader reader = new BufferedReader(new FileReader(path));
hongseyouyang 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lifeng_2009 的回复:]

File A=new File("文件名及其路径");
String filePath=A.getAbsolutePath().getPath();
[/Quote]
A.getAbsolutePath() 点不出getPath()
hongseyouyang 2010-09-09
  • 打赏
  • 举报
回复
我是想根据文件名,得到文件的绝对路径。
生活 2010-09-09
  • 打赏
  • 举报
回复
楼上说的也对。
生活 2010-09-09
  • 打赏
  • 举报
回复
File A=new File("文件名及其路径");
String filePath=A.getAbsolutePath().getPath();
stevenwenhd 2010-09-09
  • 打赏
  • 举报
回复
使用这个方法:

System.getProperty("user.dir");

里面的字符串是固定的写法
yusixdou 2010-09-09
  • 打赏
  • 举报
回复
file.getpath
a5867320 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ibm_hoojo 的回复:]

CSS code
request.getSession().getServletContext().getRealPath("/a") + "/tt.doc";
[/Quote]

+1 web应用,这样获得文件绝对路径
zyzx1986 2010-09-09
  • 打赏
  • 举报
回复

/**
* 获取项目的相对路径下文件的绝对路径
*
* @param parentDir
* 目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or
* bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径
* @param fileName
* 文件名
* @return 一个绝对路径
*/
public static String getPath(String parentDir, String fileName) {
String path = null;
String userdir = System.getProperty("user.dir");
String userdirName = new File(userdir).getName();
if (userdirName.equalsIgnoreCase("lib")
|| userdirName.equalsIgnoreCase("bin")) {
File newf = new File(userdir);
File newp = new File(newf.getParent());
if (fileName.trim().equals("")) {
path = newp.getPath() + File.separator + parentDir;
} else {
path = newp.getPath() + File.separator + parentDir
+ File.separator + fileName;
}
} else {
if (fileName.trim().equals("")) {
path = userdir + File.separator + parentDir;
} else {
path = userdir + File.separator + parentDir + File.separator
+ fileName;
}
}

return path;
}
qingtianliuyun 2010-09-09
  • 打赏
  • 举报
回复
File f = new File("a/b.txt");
String absoluteurl = f.getAbsolutePath();
我发现在IntelliJ IDEA 8.0.1中如果b.txt文件在项目src下,此时用File去操作这个绝对路径,会发现找不到文件,通过对比,发现少了src这一层目录,不知道所有的开发平台是不是这样
hoojo 2010-09-09
  • 打赏
  • 举报
回复
request.getSession().getServletContext().getRealPath("/a") + "/tt.doc";
hongseyouyang 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 shenyang6300 的回复:]

File A=new File("文件名及其路径");
A.getPath();
[/Quote]

路径 是指相对路径。。?
例如
a/tt.doc
要写成这样?

直接文件名不行?

shenyang6300 2010-09-09
  • 打赏
  • 举报
回复
File A=new File("文件名及其路径");
A.getPath();
shenyang6300 2010-09-09
  • 打赏
  • 举报
回复
File A=new File("文件名及其路径");
A.PATH();

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧