Redian新闻
>
Urgent help! Java relative file path
avatar
Urgent help! Java relative file path# Java - 爪哇娇娃
c*t
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
avatar
m*t
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?

avatar
c*t
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.

avatar
m*t
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...

avatar
c*t
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.
avatar
e*g
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.

avatar
c*t
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()
avatar
h*b
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?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。