Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

expand the ./filename to fully qualified path automatically

 
shield kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java file which takes a filename as parameter with fully qualified path(starting from root '/').
I use this java file in UNIX m/c.

This is how I execute my java file


I want here that user should be able to give just the filename as parameter not with fully qualified path.
I want that somehow the xyz class comes to know where the param.txt is located without the user specifying it's full path.
 
Sumit Bisht
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:

Remember, it is always recommended to use relative paths.
If you still want to use absolute paths, then you can create an environment variable and use relative filename.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Punit, Sumit,

f.getAbsolutePath() will search file only in the current directory (.). If you want to extend the search , possibly in all classpaths use ClassLoader getResource().

 
shield kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all guys
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18014
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, getAbsolutePath doesn't search, anywhere. It was confusing to me when I first learned Java, but a java.io.File doesn't actually represent a file, it represents a reference to a file. So you can define File objects for files and directories that don't physically exist.

If you do this:


a File object will be constructed. Since no parent directory was supplied, the current working directory will be assumed for a relative pathname supplied on the Java command line. Absolute pathnames are absolute, so if you type an absolute filename path on the command line, the path in the File object will be the file and its absolute path.

Here's a common usage:



Alternatively, you could open a FileInputStream on the File and catch FileNotFoundException.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shield kumar wrote:I want here that user should be able to give just the filename as parameter not with fully qualified path.
I want that somehow the xyz class comes to know where the param.txt is located without the user specifying it's full path.

Filenames are only unique together with the path. How shall your program handle a file /xyz/param.txt and another one /abc/param.txt?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic