Reputation: 1278
I have the java code
File file = new File(fileName);
try {
input = new FileInputStream(file.getAbsolutePath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner fileScanner = new Scanner(fin);
which I am using to read from a text file. However, I am compiling in a UNIX terminal emulator for Windows by using an ANT file and when calling
file.getAbsolutePath()
it looks for the input file in the bin
directory. How can I make it look for the input file in the src
directory?
Upvotes: 0
Views: 141
Reputation: 83577
There are several solutions to this problem:
Customize an appropriate ant task to copy the file from the src
directory to the bin
directory.
Customize an appropriate ant task to package the file into the JAR file and modify your code to load it from inside the JAR file.
Put the file in another folder, such as assets
, and customize an ant task to copy the new folder to the bin
folder.
Personally, I prefer option 3 because src
is intended to hold source files. Other assets should be stored separately, in my opinion. Also, if you copy the whole directory, it makes it easier to add additional assets to your project.
Upvotes: 1
Reputation: 474
If your fileName
is a relative path (e.g. test.txt
, not /temp/test.txt
), it looks for the file relative to the directory it is currently in. I think you can achieve your goal prepending ../src/
to your fileName
variable.
Upvotes: 1