c*****t 发帖数: 1879 | 1 Assume that I have two paths.
String fileA = "E:\\testDir\\abc";
String fileB = "..\\def.txt";
fileA is a directory.
Now, I want to find the actual path fileB resides on. How?
Also consider the case:
fileB = "F:\\temp\\def.txt";
I know that I can solve the problem by changing the current
directory to fileA, but that would cause undesired side effects.
So, any solutions?
Another question is basically the reverse. If I have
fileA = "E:\\testDir\\abc";
file | m******t 发帖数: 2416 | 2 I'm not sure I understand either of your questions. What's fileA?
Are all the relative paths relative to fileA? If that's the case,
you can try:
File fileAPath = new File(fileA);
File fileBPath = new File(fileAPath, fileB);
fileBPath.getAbsolutePath();
This isn't tested though.
【在 c*****t 的大作中提到】 : Assume that I have two paths. : String fileA = "E:\\testDir\\abc"; : String fileB = "..\\def.txt"; : fileA is a directory. : Now, I want to find the actual path fileB resides on. How? : Also consider the case: : fileB = "F:\\temp\\def.txt"; : I know that I can solve the problem by changing the current : directory to fileA, but that would cause undesired side effects. : So, any solutions?
| c*****t 发帖数: 1879 | 3 fileA is the path which I want all files in my operation to be related
to. Unfortunately, the example you showed here does not work. I've
tried it :( Because when fileB = "File\\temp\\def.txt", the result
I got was:
E:\\testDir\\abc\\F:\\temp\\def.txt
Obviously, java File class did a lazy name concatination...
【在 m******t 的大作中提到】 : I'm not sure I understand either of your questions. What's fileA? : Are all the relative paths relative to fileA? If that's the case, : you can try: : File fileAPath = new File(fileA); : File fileBPath = new File(fileAPath, fileB); : fileBPath.getAbsolutePath(); : This isn't tested though.
| m******t 发帖数: 2416 | 4
Hmm... the javadoc for File.File() does look suspicious.
I tested this code:
import java.io.*;
public class RelativePath
{
public static void main(String args[]) throws IOException
{
File fileAPath = new File(args[0]);
File fileBPath = new File(fileAPath, args[1]);
System.out.println(fileBPath.getAbsolutePath());
System.out.println(fileBPath.getCanonicalPath());
}
}
When running from command line, this works well:
$ java RelativePath I:\scratch ..
I:
【在 c*****t 的大作中提到】 : fileA is the path which I want all files in my operation to be related : to. Unfortunately, the example you showed here does not work. I've : tried it :( Because when fileB = "File\\temp\\def.txt", the result : I got was: : E:\\testDir\\abc\\F:\\temp\\def.txt : Obviously, java File class did a lazy name concatination...
| c*****t 发帖数: 1879 | 5 This is exactly the problem that I am facing. Unless that I write my own
specific parser, there seemed to be no way to do it. Too bad, my program
needs to be run on Windows, SGI and Mac :(
Boy, it is a pain in the rear to write it. | e***g 发帖数: 158 | 6 File.isAbsolute()
【在 c*****t 的大作中提到】 : This is exactly the problem that I am facing. Unless that I write my own : specific parser, there seemed to be no way to do it. Too bad, my program : needs to be run on Windows, SGI and Mac :( : Boy, it is a pain in the rear to write it.
| c*****t 发帖数: 1879 | 7 The problems are
1. how to fileB's path s.t. it is relative to fileA's path
2. vice versa. Given fileA's path and fileB's relative path (may not
relative if on a different drive), then obtain fileB's true path.
【在 e***g 的大作中提到】 : File.isAbsolute()
| h******b 发帖数: 312 | 8
~~~~~~~ .. means up one directory, I don't see any
relation btw fileB and fileA
【在 c*****t 的大作中提到】 : Assume that I have two paths. : String fileA = "E:\\testDir\\abc"; : String fileB = "..\\def.txt"; : fileA is a directory. : Now, I want to find the actual path fileB resides on. How? : Also consider the case: : fileB = "F:\\temp\\def.txt"; : I know that I can solve the problem by changing the current : directory to fileA, but that would cause undesired side effects. : So, any solutions?
|
|