在java中获取文件的路径 [英] get path of a file in java

查看:187
本文介绍了在java中获取文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,但我无法弄清楚如何解决它。
我有一个文件夹中的文本文件:
C:/aaa/bbb/ccc/ddd/test.txt
文本文件夹中文件夹中的excel文件:
C:/aaa/bbb/ccc/ddd/excelFiles/test.xls
文本和excel文件都具有相同的名称。

My problem is very simple and yet I can't figure out how to solve it. I have text files in a folder: "C:/aaa/bbb/ccc/ddd/test.txt" And excel files in a folder within the text files folder: "C:/aaa/bbb/ccc/ddd/excelFiles/test.xls" Both text and excel files have the same name.

I想要能够访问这些excel文件的路径。
我做的是:
this.file.getParent()+\\+excelFiles+\\+ file.getName() .substring(0,fileName.indexOf('。'))+。xls

I would like to be able to access the path of those excel files. What I did is: this.file.getParent()+"\\"+"excelFiles"+"\\"+file.getName().substring(0, fileName.indexOf('.'))+".xls"

我得到一个字符串索引超出范围错误。
谢谢你的帮助:)

I get a "String index out of range" error. Thank you for your help :)

推荐答案

如果我明白你的问题,一个选项是使用 File.getCanonicalPath( ) 喜欢,

If I understand your question, one option is to use File.getCanonicalPath() like,

try {
    File f = new File("C:/aaa/bbb/ccc/ddd/excelFiles/test.xls");
    System.out.println(f.getCanonicalPath());
} catch (IOException e) {
    e.printStackTrace();
}

这篇关于在java中获取文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆