-1

I want to dynamically set the document root in a program in Java. My Java program accepts 2 inputs:

  1. document root path as String
  2. an Int which I'm using for some calculations.

I set this document root in program, so that any use of File file=new File(filename) will fetch the specified file from the document root path:

String f_path=args[0]; //within main().

f_path could be any path in that system other than the root directory of the program. Currently, when I do File file=new File(filename); it searches for the specified file in the program's root directory. How can I change this to f_path?

developer3
  • 75
  • 2
  • 9

2 Answers2

1

You should craft absolute paths, for example using the File(String parent, String path) constructor :

new File(f_path, filename)
Aaron
  • 24,009
  • 2
  • 33
  • 57
1

You may use the File constructor which takes a parent parameter : File(String parent, String child ) , e.g :

File file=new File(f_path, fileName);
Arnaud
  • 17,229
  • 3
  • 31
  • 44