How can I just use a relative filename instead of an absolute filename?
I have a program reading a .txt file and I don't want to use an absolute filename because it is machine dependent and I just want to use a relative filename. I do not know how to do that. Here's the part of my program I'm 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 did it simply:
"C:/FourLetterWords.txt"
then my catch exception pops up and says file not found. But I just want to use ...
"FourLetterWords.txt"
source to share
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 "user.home" on every platform supported by Java and then add "Documents / FourLetterWords.txt" to that path.
source to share
What they said, plus here are some additional useful tricks for navigating up and down folders:
//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;
source to share
Using relative file paths is related to the path you are currently in. Just guess here, but try "Documents / FourLetterWords.txt".
If it works, the reason is that your current directory is "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".
source to share
There are two scripts for accessing files in Java or any programming language:
and. The file is present in the project directory.
b. The file is outside the project directory.
a. The file is present in the project directory:
The file is present in the bundle or placed in a folder in the project directory. In this case, you can use something like
String fileName = "/resourceFolder/FourLetterWords.txt";
The resource folder is a child of the project root folder.
b. The file is present in a directory outside the project directory
You should have a property set that will always have the path in which you will have the file. You must install it as part of the env for the code to always work on all operating systems. You can also install it as part of a properties file, which will change for each operating system.
Getting a system variable: System.getEnv("FILE_DIR");
Getting a property:System.getProperty("file.dir");
source to share