| Author |
Getting file name from a directory path
|
Raghu Nathan
Ranch Hand
Joined: Jan 16, 2006
Posts: 44
|
|
Hello everyone, I need some help in determining the filename from a given directory path. After gettign that I have to save it in the DB with a different path. I dont need to physically save the file in the other path but just get the file name and prefix that with the other dir path. Say, the file that I locate in my c:\ is in c:\MyDir\doc1.doc and I need to save it in the DB with the UNIX dir reference /unix/dir/. So it should get saved as /unix/dir/doc1.doc in the database. Now, I had used the following code to do that: String dirPath = "/unix/dir/"; File filePath = new File(brBean.getRep_dir_path()); String fileName = filePath.getName(); String varFile = new String(dirPath + fileName); details.setRepDirPath(varFile); //my data bean System.out.println("Original Path:" + filePath); System.out.println("Changed Path:" + varFile); This seemed to work fine when I was doing in my development environment where everything was in Windows. When I do the same in the other environments, where the application runs on unix, I get a different result. What happens with the above code is that it prefixes the UNIX dir path to the whole path identified. So the end result is: /unix/dir/c:\MyDir\doc1.doc I was wondering if one way to fix this would be to use some string function(s) and get the file name somehow. So you'd have doc1.doc after eliminating the path in which it was identified. And then prefix the UNIX path to the file name. Is that the most optimum solution? I hope I have been articulate enough to explain my problem. Also, I do not know if this is the right forum for this question. If its not, then excuse me and please direct me to the right forum.
|
Thanks ... Raghu
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Extracting the filename portion, given a full pathname, is very similar to another question in this forum... So... I am going to modify the solution from that thread... Here is how you extract the file name portion, given the full name of the file... BTW, since I modified the code from another thread, I haven't tested it -- please test it first. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Madhu Enugan
Greenhorn
Joined: Oct 19, 2009
Posts: 2
|
|
Raghu,
I am running into same issue. Have you figured out a fix for this?
Please share.
Thanks.
Raghu Nathan wrote:Hello everyone,
I need some help in determining the filename from a given directory path. After gettign that I have to save it in the DB with a different path. I dont need to physically save the file in the other path but just get the file name and prefix that with the other dir path.
Say, the file that I locate in my c:\ is in c:\MyDir\doc1.doc and I need to save it in the DB with the UNIX dir reference /unix/dir/.
So it should get saved as /unix/dir/doc1.doc in the database.
Now, I had used the following code to do that:
String dirPath = "/unix/dir/";
File filePath = new File(brBean.getRep_dir_path());
String fileName = filePath.getName();
String varFile = new String(dirPath + fileName);
details.setRepDirPath(varFile); //my data bean
System.out.println("Original Path:" + filePath);
System.out.println("Changed Path:" + varFile);
This seemed to work fine when I was doing in my development environment where everything was in Windows. When I do the same in the other environments, where the application runs on unix, I get a different result.
What happens with the above code is that it prefixes the UNIX dir path to the whole path identified. So the end result is: /unix/dir/c:\MyDir\doc1.doc
I was wondering if one way to fix this would be to use some string function(s) and get the file name somehow. So you'd have doc1.doc after eliminating the path in which it was identified. And then prefix the UNIX path to the file name.
Is that the most optimum solution?
I hope I have been articulate enough to explain my problem.
Also, I do not know if this is the right forum for this question. If its not, then excuse me and please direct me to the right forum.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
How about Henry's solution? It probably worked 3 years ago as Raghu Nathan hasn't replied afterwards stating it didn't.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Madhu Enugan
Greenhorn
Joined: Oct 19, 2009
Posts: 2
|
|
That worked. But that is a workaround. I would like to know the fix for the original problem.
Thanks anyway.
Rob Prime wrote:How about Henry's solution? It probably worked 3 years ago as Raghu Nathan hasn't replied afterwards stating it didn't.
|
 |
 |
|
|
subject: Getting file name from a directory path
|
|
|