简体   繁体   English

我怎样才能使用相对文件名而不是绝对文件名?

[英]How can I just use the relative file name instead of the absolute file name?

I am having my program read a .txt file and I don't want to use the absolute file name because it is machine dependent and I just want to use the relative file name. 我正在让我的程序读取.txt文件,我不想使用绝对文件名,因为它取决于机器,我只想使用相对文件名。 I don't know how to get it to do that. 我不知道如何做到这一点。 Here is the part of my program I am talking about: 这是我正在讨论的程序的一部分:

private List<String> readFile() {

    List<String> wordsList = new ArrayList<>();

    try {
        String fileName = "C:/Users/Phil/Documents/FourLetterWords.txt";
        File fourLetterWords = new File(fileName);
        Scanner in = new Scanner(fourLetterWords);

        while (in.hasNextLine()) {
            String line = in.nextLine();
            if (line!=null && !line.isEmpty()) {
                wordsList.add(line);
            }
        }
    } 
    catch (FileNotFoundException ex) {
        System.out.println("File not found.");
    }
    return wordsList ;
}

If I were to make it just: 如果我只是这样做:

"C:/FourLetterWords.txt"

then my catch exception comes in and says File not found. 然后我的捕获异常进来并说File找不到。 But I really just want to use... 但我真的只想用...

"FourLetterWords.txt"

Change this 改变这个

String fileName = "C:/Users/Phil/Documents/FourLetterWords.txt";

to something like 喜欢的东西

File f = new File(System.getProperty("user.home"),
    "Documents/FourLetterWords.txt");

which will work to get the "user.home" on every platform that Java supports, and then append "Documents/FourLetterWords.txt" to that path. 这将在Java支持的每个平台上获得“user.home”,然后将“Documents / FourLetterWords.txt”附加到该路径。

you can either 你也可以

  1. pass the filename as an parameter 将文件名作为参数传递
  2. You can load files from the same directory as the .class file with getResourceAsStream(). 您可以使用getResourceAsStream().从与.class文件相同的目录加载文件getResourceAsStream().
  3. System.getProperty("user.dir") returns the current directory System.getProperty("user.dir")返回当前目录

What they said, plus here's some additional useful tricks for navigating up and down folder levels: 他们说了什么,加上这里有一些额外的有用技巧,用于在文件夹级别上下导航:

//absolute path from where application has initialized
String target = System.getProperty("user.dir"); 

//drop the last folder to go down one level
target = target.substring(0, target.lastIndexOf(File.separator)); 

//go into another directory
target = target + File.separator + targetFolder; 

//use it
return target;

Using relative file paths has to do with the path you are currently in. Just guessing here, but try "Documents/FourLetterWords.txt". 使用相对文件路径与您当前所在的路径有关。只需在此猜测,但请尝试“Documents / FourLetterWords.txt”。

If that works, the reason is because your current directory is "C:\\Users\\Phil" 如果这样可行,原因是因为您当前的目录是“C:\\ Users \\ Phil”

if not try 如果不试试

System.out.println("Working Directory = " + System.getProperty("user.dir"));

then try moving the file there to be able to use "FourLetterWords.txt". 然后尝试移动文件,以便能够使用“FourLetterWords.txt”。

There are 2 scenarios for file access in Java or any programming language: Java或任何编程语言有两种文件访问方案:

a. 一个。 The file is present with in the project directory. 该文件存在于项目目录中。
b. File is out of the project directory. 文件不在项目目录中。

a. 一个。 File present with in the project directory: 项目目录中的文件:

File is present bundled or pushed to folder in project directory. 文件存在捆绑或推送到项目目录中的文件夹。 In this case you could use some thing like 在这种情况下,你可以使用一些东西
String fileName = "/resourceFolder/FourLetterWords.txt";

The resource folder is child of the root folder of the project. 资源文件夹是项目根文件夹的子项。

b. File is present at location out of project directory 文件出现在项目目录之外的位置

You need to have a property set, which will always have the path where you will be having the file. 您需要拥有一个属性集,该属性集始终具有您将拥有该文件的路径。 You need to set it as part of the env so that the code will always run on all operating systems. 您需要将其设置为env的一部分,以便代码始终在所有操作系统上运行。 You can also set it as part of the property file which will be changing for each operating system. 您还可以将其设置为属性文件的一部分,该文件将针对每个操作系统进行更改。

Getting system variable: System.getEnv("FILE_DIR"); 获取系统变量: System.getEnv("FILE_DIR");
Getting property: System.getProperty("file.dir"); 获取属性: System.getProperty("file.dir");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM