从绝对路径转换为相对路径

问题描述 投票:0回答:3

我想将此相对路径/home/cce2050/Music/part1/ints10000.dat转换为其绝对路径。任何人都可以向我推荐这条路吗?

public static String[] split() throws FileNotFoundException, IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/home/cce2050/Music/part1/ints10000.dat")));

    String line;

    String[] aList = new String[10000];

    while ((line = reader.readLine()) != null) {

        aList = line.split("\\s+");

    }

    return aList;

}
java linux fedora relative-path absolute-path
3个回答
0
投票

我个人认为你对相对/绝对路径的理解是错误的。绝对路径指定从根/到文件的路径,而相对路径指定从当前目录(位置)到指定文件的路径。

您提供的路径已经是绝对路径。


0
投票

你错过了绝对和相对路径之间的混淆,所以我想你问的是:

./Music/part1/ints10000.dat

0
投票

如果您希望将相对路径转换为绝对路径,我建议使用File.getCanonicalPath()您可以在其上查看文档here。此外,您可以阅读更多关于相对和绝对路径转换here

所以,如果你想找到相对路径,你可以写下这样的东西:

String absolutePath = (new File("Your/Relative/Path")).getCanonicalPath()

话虽这么说,让我们知道Unix系统绝对文件路径是从/home目录引用的。您指定的文件路径可能已经是绝对路径

© www.soinside.com 2019 - 2024. All rights reserved.