由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Urgent help! Java relative file path
相关主题
No decent way for input password from command line.紧急求教,JAVA程序如何启动浏览器
这段程序的输出是什么? 为什么BufferedWriter里的write()
新手请教一个问题关于char和int的问题
JAVA APPLICATION PATH 一问javaMail的问题
Re: 如何从键盘输入获得一个float值?谢谢!傻问题,关于java里的资源释放
Stupid IBM JDKJAVA文本文件读写问题
怎么从键盘输入整数或float?刚刚开始学习java,麻烦帮我看一下我哪里错了行吗?谢谢
是否可以通过Java的程序直接导入网页?请问一下
相关话题的讨论汇总
话题: filea话题: file话题: fileb话题: path话题: relative
进入Java版参与讨论
1 (共1页)
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?

1 (共1页)
进入Java版参与讨论
相关主题
请问一下Re: 如何从键盘输入获得一个float值?谢谢!
从文件读入数据得到的是bytesStupid IBM JDK
新手问一个多线程的问题怎么从键盘输入整数或float?
very weird problem是否可以通过Java的程序直接导入网页?
No decent way for input password from command line.紧急求教,JAVA程序如何启动浏览器
这段程序的输出是什么? 为什么BufferedWriter里的write()
新手请教一个问题关于char和int的问题
JAVA APPLICATION PATH 一问javaMail的问题
相关话题的讨论汇总
话题: filea话题: file话题: fileb话题: path话题: relative